The Chicago Boss API is mostly stable, but still might change before 1.0.
WebSockets are an HTML5 technology used for two-way messaging from inside a web page. As of version 0.8.0, Chicago Boss provides infrastructure for defining one or more WebSocket controllers. Note that WebSockets are only supported with Cowboy, so put {server, cowboy}
in your boss.config before attempting anything on this page.
The server WebSocket API is based around Erlang message-passing; to send a message to a particular client, simply send it a message like:
Furthermore, you can broadcast a message that will be handled by your handle_broadcast/2 function by using the module name of your websocket module and the proper method of the boss_service_worker module:
To handle incoming messages from clients, you'll need to create WebSocket controllers in your project's src/websocket
directory. Each controller module should have a name of the form <app name>_<service name>_websocket.erl
and be a parameterized module with parameters for Req
and SessionId
, e.g.:
The module should implement the boss_service_handler
behavior. The boss_service_handler
behavior consists of the following six functions:
Initialize the service.
Handle a client joining a service.
Handle a client leaving a service.
Handle an incoming message from a client.
Handle an outgoing broadcast message from some Erlang process to the connected web sockets. It is your responsibility to keep track of the web sockets (via handle_join and handle_close) and send the outgoing messages as you see fit.
Handle an informational message sent to the underlying gen_server process.
Perform any cleanup before shutting down the service.
WebSocket URLs are automatically generated from the WebSocket controllers in your project's src/websocket
directory. For example, if you have myapp_foobar_websocket.erl
, then to create a new WebSocket on the client:
The WebSocket URLs respect the base_url
config parameter. For example, if the base_url
is set to "/chat", then you would use this code instead:
For a reference on the WebSocket client programming, see Mozilla's WebSockets page.