Skip to content

Instantly share code, notes, and snippets.

@hugosp
Created April 4, 2016 22:37
Show Gist options
  • Save hugosp/5eeb2a375157625e21d33d75d10574df to your computer and use it in GitHub Desktop.
Save hugosp/5eeb2a375157625e21d33d75d10574df to your computer and use it in GitHub Desktop.
Minimal express-ws broadcast to all clients
var express = require('express');
var expressWs = require('express-ws');
var expressWs = expressWs(express());
var app = expressWs.app;
app.use(express.static('public'));
var aWss = expressWs.getWss('/');
app.ws('/', function(ws, req) {
console.log('Socket Connected');
ws.onmessage = function(msg) {
console.log(msg.data);
aWss.clients.forEach(function (client) {
client.send(msg.data);
});
};
});
app.listen(3444);
@howmuch515
Copy link

GJ!

@vsTianhao
Copy link

get

@MiopeCiclope
Copy link

I create a fork to enable fetching clients from a route like old getWss(route)

If anyone needs this feature check examples/url-filter-broadcast.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment