Last active
October 13, 2022 12:55
-
-
Save i-Robi/e290108b21b90ea687b9 to your computer and use it in GitHub Desktop.
Separating clients from the admin panel with socket.io and namespaces
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
// 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