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

TestBox Latest Updates and News!

TestBox Latest Updates and News!

We’re thrilled to have launched the new TestBox website and TestBox 6.0! If you haven’t had a chance to explore yet, visit TestBox to discover updated documentation, powerful resources, and features that make testing more efficient than ever.

Maria Jose Herrera
Maria Jose Herrera
November 21, 2024
Is Your ColdFusion Application Ready for the Future?

Is Your ColdFusion Application Ready for the Future?

In a rapidly evolving digital world, maintaining performance, security, and scalability for ColdFusion applications is more challenging than ever. Whether you're using Lucee or Adobe ColdFusion, legacy systems can become a bottleneck for growth, innovation, and user satisfaction. The need to future-proof your ColdFusion applications has never been more critical.

But where do you start?


The Hidden Costs of an Outdated ColdFusion Application

As you...

Cristobal Escobar
Cristobal Escobar
November 21, 2024
The Hidden Costs of In-House Database Management

The Hidden Costs of In-House Database Management

The Hidden Costs of In-House Database Management


Opting for in-house database management involves more than just a salary. Here are some often-overlooked costs associated with maintaining your own DBA team.



1. High Salaries and Benefits


Hiring skilled DBAs is expensive. According to industry reports, the average salary of a DBA in the U.S. can range from $85,000 to over $130,000 per year, depending on experience and expertise. When you add ...

Cristobal Escobar
Cristobal Escobar
November 20, 2024