To add real-time data to your app, Joystick supports the creation of WebSocket servers via the websockets
property on the options object passed to joystick.app()
.
Example Usage
/index.server.js
import joystick from '@joystick.js/node';
joystick.app({
websockets: {
example_endpoint: {
on_open: (connection = {}) => {
// Handle connection open event...
},
on_message: (message = {}, connection = {}) => {
// Handle an inbound client message...
},
on_close: (code = 0, reason = '', connection = {}) => {
// Handle a client close event...
},
},
},
routes: { ... },
});
In this example, we define a WebSocket server at example_endpoint
.
Joystick automatically wires up the server and makes it accessible to clients at:
ws://localhost:2600/api/_websockets/example_endpoint
in development.wss://<your-domain>/api/_websockets/example_endpoint
in production.
Once defined, you can connect to it from a Joystick component.
API
Definition
{
websockets: {
[websocket_endpoint: string]: {
on_open: (connection: object) => void,
on_message: (message: object, connection: object) => void,
on_close: (code: integer, reason: string, connection: object) => void,
}
}
}
Parameters
- websocket_endpoint object required
-
An object defining the WebSocket server at the given endpoint.
- on_open function
- A function called when a WebSocket client connects to this endpoint.
- on_message function
- A function called when a WebSocket client sends a message to this endpoint.
- on_close function
- A function called when a WebSocket client disconnects from this endpoint.