Blog

Introducing SocketBox, a new WebSocket library

Maria Jose Herrera October 10, 2024

Spread the word

Maria Jose Herrera

October 10, 2024

Spread the word


Share your thoughts

We’ve been working on a fun new library designed to make WebSockets easier for CFML (and BoxLang) developers. WebSockets are incredibly powerful for real-time applications and it’s honestly an area that CF developers have lagged far behind other languages in using. We’ve wanted something easy like Socket.io which can just be dropped into Node apps, but the problem with CF is how it’s deployed typically to a servlet and a library can’t easily modify the behavior of the HTTP listeners, which requires an integration embedded into the servlet itself.

Introducing SocketBox, a new approach to easy peasy websockets that comes in two parts:

  • A new WebSocket handler implemented at the HTTP listener level which is now baked into the latest builds of CommandBox 6.1-rc AND the BoxLang MiniServer beta-16 builds. This provides the low-level logic required to negotiate the protocol upgrade and websocket handshake at the HTTP level.
  • A new CF module that sits in your application to send and receive messages over the websocket connections.

The HTTP handler portion is quite simple. In CommandBox, you can configure 3 parts of it

  • Whether it’s enabled or not (default disabled)
  • What URI it will respond to. Default is yoursite.com/ws
  • The path inside your web root to the listener CFC to receive messages through (default /WebSocket.cfc

Right now the BoxLang Miniserver uses the defaults above with the exception of being enabled by default.

The only hard requirement of using SocketBox is YOU MUST BE RUNNING YOUR SERVER ON COMMANDBOX OR THE BOXLANG MINISERVER! This is because of the special low-level HTTP WebSocket handler we’re providing there. Out side of that, you can use SocketBox on

  • Lucee Server apps
  • Adobe ColdFusion apps
  • BoxLang apps

because there is no specific code or functionality to any particular CF engine. This is because all incoming messages are routed internally to look like a remote CFC method, which is a core feature of all 3 CF engines. To configure the listener, install SocketBox

CommandBox>install socketbox

SocketBox is laid out as a ColdBox module, but does NOT require ColdBox to use. You can use SocketBox in any CF app.

Next, create your listener CFC in the web root (or whatever location you choose, so long as you configure CommandBox for the custom location)

/WebSocket.cfc

/**
 * I facilitate receiving an sending all WebSocket messages.
 * I need to be web-accessible, and extend the WebSocketCore class from SocketBox4.
 */
componentextends="modules.socketbox.models.WebSocketCore" {

/**
     * All incoming websocket messages flow through me.
     *
     *@message The text of the incoming message.
     *@channel The Java channel object representing the user connection.
     *   Use this object to send messages back to the same channel later.
     */functiononMessage( required String message, required channel ) {
if( message EQ "Ping" ) {
sendMessage( "Pong", arguments.channel );
        }
    }

}

Your onMessage() method does NOT need to be remote. All incoming messages are funnelled through an existing remote method in the superclass and then funneled to your method.

The sendMessage() method above can be used anywhere in your application, so long as yu have a reference to the channel object of an existing user connection. You can get those by listening to the onConnect() listener method.

You can send a broadcast message to all connected users like so:

new WebSocket().broadcastMessage( "Don't forget to tip your waiters and waitresses" );

We have lots of plans for SocketBox, including addition of a STOMP core listener which will have topic subscription semantics, auth, and more, but for right now you can start playing around with basic websocket messaging as you wish.

Since any WebSocket discussion is not complete without the obligatory chat room, I have deployed a simple example of SocketBox for you to play with.

https://socketbox-demo.onrender.com/

It’s hosted for free on

render.com

, so please be patient if it takes a few seconds to spin up. It will go back to sleep after a few minutes if no one is hitting it. Please be polite in the chat-- your messages are visible to anyone using the demo at the same time, so don’t make me regret putting this public

The demo is


The code is all quite simple. I’m not using any special client side JS libraries, just the native browser support for WebSockets.

If you’re interested in hosting a site on render, check out the render.yml and Dockerfile in the root of the demo repo to see how I got it up and running with minimal configuration.

You can also play with this demo locally by cloning the code repo and starting up the site on the latest BoxLang MiniServer, or the latest CommandBox 6.1-rc builds with web.websocket.enable=true in your server.json.

Add Your Comment

Recent Entries

Partner with BoxLang and Ortus at Into the Box 2025: Empowering the Future of Modern Software Development!

Partner with BoxLang and Ortus at Into the Box 2025: Empowering the Future of Modern Software Development!

At Ortus Solutions, we’ve always been at the forefront of innovation in the ColdFusion ecosystem. From pioneering modern ColdFusion practices to developing cutting-edge tools and frameworks, we’ve been passionate to help and sup[port the community into shaping the future of web development.That’s why we decided to build BoxLang, our new JVM programming language that not only builds on the strengths of ColdFusion but takes modern software development to the next level.

Maria Jose Herrera
Maria Jose Herrera
December 23, 2024
Why BoxLang When You Have Kotlin, Groovy, Scala, and more…

Why BoxLang When You Have Kotlin, Groovy, Scala, and more…

As we approach a stable release of BoxLang and our continued marketing reaches more folks, many have asked about its purpose. Why create a new language when the JVM ecosystem already includes established languages like Kotlin, Groovy, and Scala, to name a few.

Luis Majano
Luis Majano
December 18, 2024