Skip to content

Broadcast? #30

Answered by kartikk221
debshaw13 asked this question in Q&A
Dec 13, 2021 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

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:

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…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@debshaw13
Comment options

Answer selected by kartikk221
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants