Technical Note: Using WebSockets

Starting in v4.0, Aloe Express includes support for the WebSocket Protocol, enabling two-way communication between clients and an Aloe Express-based server. The implementation is based on RFC 6455.

Aloe Express's WebSocket support is designed to be seamless. To add WebSocket support to an app, simply have the client open a WebSocket connection and reference an endpoint. In your app, route WebSocket-related requests just as you would HTTP requests, evaluate them, and process them.

For security, you might want to evaluate the Origin header before handshaking and continuing with a WebSocket connect request. You should respond with a "403 Forbidden" status if the request is being denied.

To handshake with a client, simply call Request.WSHandshake. From that point on, messages received on the WebSocket that will continue to be routed to the original endpoint. Aloe will automatically manage and decode an incoming message. It will fully compose and decode an incoming message, store it in the Request's Body property, and pass it to the app's RequestHandler method.

To respond to a message, or to send a message to the client for any reason, use the Request.WSMessageSend method. All frame encoding and message composition is handled by the method.

For additional information about Aloe Express's WebSocket support, please refer to the v4.0.0 release notes in the AloeExpress module.

For a demonstration of Aloe Express's WebSocket support, see the DemoWebSockets demo module that is included with the Aloe Express distribution project.