Reputation: 628
I setup a multi room socket.io / nodeJS chat server using this single chat room example as a reference:
https://raw.githubusercontent.com/socketio/socket.io/master/examples/chat/index.js
I used this doc to modify the code from single room to multiple rooms:
https://socket.io/docs/rooms-and-namespaces/
I also modified the code to use HTTPS on port 8080 instead of HTTP.
I have the multiple rooms implemented. However, as new users join different chat rooms, their messages are sent to all other chats. I think this may be because everyone gets joined the the original room. Example from my node console log:
Joining: test //user1
Joining: test //user2
Joining: test2 //user3
user1 said something in test
user2 is typing in test
user3 said something in test
Rooms should be getting joined dynamically via a URL GET param such as ?room=room123
Here is the nodeJS index.js code. The client side code is pretty much the same as in the github link above. I'm fairly new to node and JS. Any ideas on whats happening?
// Setup basic express server
var express = require('express');
var app = express();
var path = require('path');
var fs = require('fs');
var https = require('https');
var url = require('url');
var privateKey = fs.readFileSync('/etc/letsencrypt/live/hostname/privkey.pem', 'utf8');
var certificate = fs.readFileSync('/etc/letsencrypt/live/hostname/fullchain.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var server = https.createServer(credentials, app);
var io = require('socket.io')(server);
var port = process.env.PORT || 8080;
/* CONSOLE COLORS */
var GREEN = '\033[0;32m';
var LBLUE = '\033[1;34m';
var BLUE = '\033[0;34m';
var RED = '\033[0;31m';
var YELLOW = '\033[0;33m';
var PURPLE = '\033[0;35m';
var NC = '\033[0m'; // No Color Reset
/* Chatroom Settings */
var numUsers = 0;
var roomID = null;
server.listen(port, function () {
console.log('HTTPS Server listening on port ' + GREEN + '%d' + NC, port);
});
/* Routing */
app.use(express.static(path.join(__dirname, 'public')));
chat();
function chat(){
// multiple rooms
io.on('connection', function(socket){
var addedUser = false;
var referer = url.parse(socket.handshake.headers.referer);
var query = (referer.query).split("=");
roomID = query[1];
socket.join(roomID);
/* // attempt to resolve issue of sending messages to all rooms
console.log(socket.adapter.sids);
if(Object.keys(socket.adapter.sids).length < 1){
console.log('Connecting to room:' + roomID);
socket.join(roomID);
}
*/
console.log("Joining: " + YELLOW + roomID + NC);
//console.log("UUID: " + Object.keys(socket.adapter.nsp.connected)[0]);
//console.log("unique room id:" + Object.keys(socket.adapter.sids));
// when the client emits 'new message', this listens and executes
socket.on('new message', function (data) {
// we tell the client to execute 'new message'
socket.to(roomID).emit('new message', {
username: socket.username,
message: data
});
console.log(socket.username + ' said something in \t' + YELLOW + roomID + NC);
});
// when the client emits 'add user', this listens and executes
socket.on('add user', function (username) {
if (addedUser) return;
// we store the username in the socket session for this client
socket.username = username;
++numUsers;
addedUser = true;
socket.emit('login', {
numUsers: numUsers,
});
// echo to room that a person has connected
socket.to(roomID).emit('user joined', {
username: socket.username,
numUsers: numUsers
});
});
// when the client emits 'typing', we broadcast it to others
socket.on('typing', function () {
console.log(socket.username + ' is typing in \t' + YELLOW + roomID + NC);
socket.to(roomID).emit('typing', {
username: socket.username
});
});
// when the client emits 'stop typing', we broadcast it to others
socket.on('stop typing', function () {
socket.to(roomID).emit('stop typing', {
username: socket.username
});
});
// when the user disconnects.. perform this
socket.on('disconnect', function () {
console.log(socket.username + PURPLE + ' disconnected' + NC
+ ' from ' + YELLOW + roomID + NC);
if (addedUser) {
--numUsers;
// echo that this client has left
socket.to(roomID).emit('user left', {
username: socket.username,
numUsers: numUsers
});
}
});
});
}
Upvotes: 0
Views: 1117
Reputation: 628
The issue ended up being caused by the global "var roomID" variable. Once I used the query[1] variable in all the socket.to(query[1]).emit functions it worked.
Upvotes: 0
Reputation: 7738
I see in your code that all sockets use the same roomID
variable. Each of them needs to have their own roomID
variable.
Upvotes: 1