Broadcast? #30
-
Does hyper-express have an equivalent function to broadcast (that is available in ws)? For example, using ws, the following code broadcasts data without any connections:
If I replace 'WebSocket' above with 'HyperExpress' (after importing 'hyper-express' of course), I see the following error:
Would appreciate any advice as to what I am doing wrong. Thank you for the great package, and apologies if this is a newbie question. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You could go about this two ways. One method would be where you keep track of all incoming websocket connections and then create your own "broadcast" method which dispatches the data to the clients. const connections = {};
const uuid_v4 = require('crypto').randomUUID;
const webserver = new HyperExpress.Server();
function ws_broadcast(data){
Object.keys(connections).forEach((id) => {
connections[id].send(data);
});
}
webserver.ws('/connect', (ws) => {
// Assign some random identifier to the connection
ws.id = uuid_v4();
// Store the connection in our connections object
connections[ws.id] = ws;
// Remove the connection from our object once it is closed
ws.on('close', () => {
delete connections[ws.id];
});
}); The second way is to use the MQTT based pub/sub system which is documented in our documentation. |
Beta Was this translation helpful? Give feedback.
You could go about this two ways. One method would be where you keep track of all incoming websocket connections and then create your own "broadcast" method which dispatches the data to the clients.
Example: