Skip to content

Instantly share code, notes, and snippets.

@i-Robi
Last active October 13, 2022 12:55
Show Gist options
  • Save i-Robi/e290108b21b90ea687b9 to your computer and use it in GitHub Desktop.
Save i-Robi/e290108b21b90ea687b9 to your computer and use it in GitHub Desktop.
Separating clients from the admin panel with socket.io and namespaces
// Server
var admin = io.of('/admin'),
client = io.of('');
admin.on('connection', function (socket) {
socket.on('message', function(m) {
console.log(m);
});
admin.emit('message1', 'Message1: admin to admin');
client.emit('message1', 'Message1: admin to client');
});
client.on('connection', function(socket) {
socket.on('message', function(m) {
console.log(m);
});
admin.emit('message2', 'Message2: client to admin');
client.emit('message2', 'Message2: client to client');
});
// Client: index.js
var socket = io.connect(document.URL);
socket.on('connect', function() {
socket.emit('message', 'Message: sent by socket.emit');
});
socket.on('message1', function(m) {
console.log(m);
});
socket.on('message2', function(m) {
console.log(m);
});
// Client: admin.js
var socket = io.connect(document.URL);
socket.on('connect', function () {
socket.emit('message', 'connectedToAdmin');
});
socket.on('message1', function(m) {
console.log(m);
});
socket.on('message2', function(m) {
console.log(m);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment