-
-
Save leenorshn/731612d98f810b2fdbd6dbff5fdabcfa to your computer and use it in GitHub Desktop.
Minimal express-ws broadcast to all clients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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