Skip to content

Instantly share code, notes, and snippets.

@leenorshn
Forked from hugosp/app.js
Created September 8, 2020 13:15
Show Gist options
  • Save leenorshn/731612d98f810b2fdbd6dbff5fdabcfa to your computer and use it in GitHub Desktop.
Save leenorshn/731612d98f810b2fdbd6dbff5fdabcfa 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment