Skip to content

Instantly share code, notes, and snippets.

@Tomoli75
Forked from hugosp/app.js
Created October 11, 2019 15:59
Show Gist options
  • Save Tomoli75/c9da1bc6959de91c509d076c7d0cda5d to your computer and use it in GitHub Desktop.
Save Tomoli75/c9da1bc6959de91c509d076c7d0cda5d 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