I finally managed to work with socket.io namespace stuff which I'm using for building a chat module. Here, employees of multiple organizations can join and vhat with other employees of the respective organization. What I'm doing at here is creating separate namespaces for each organization. So, it'll be easier for me to manage all employees of different organizations.
Here is my server side code:
var express = require('express'),
http = require('http'),
app = express(),
server = http.createServer(app),
io = require('socket.io').listen(server);
var nsp_1005 = io.of('/nsp_bucket_1005');
nsp_1005.on('connection', function(socket){
console.log('someone connected to namespace bucket 1005');
socket.on('addEmp', function(login_org_id, login_emp_id, login_emp_name){
console.log('addEmp - Org_Id : '+login_org_id);
console.log('addEmp - Emp_Id : '+login_emp_id);
console.log('addEmp - Emp_Name : '+login_emp_name);
});
socket.on('disconnect', function(){
console.log('Someone disconnected from namespace bucket 1005.');
});
});
var nsp_1010 = io.of('/nsp_bucket_1010');
nsp_1010.on('connection', function(socket){
console.log('someone connected to namespace bucket 1010');
socket.on('addEmp', function(login_org_id, login_emp_id, login_emp_name){
console.log('addEmp - Org_Id : '+login_org_id);
console.log('addEmp - Emp_Id : '+login_emp_id);
console.log('addEmp - Emp_Name : '+login_emp_name);
});
socket.on('disconnect', function(){
console.log('Someone disconnected from namespace bucket 1010.');
});
});
Those 1005, 1010 codes are Organization IDs. Sorry for the wired naming scheme. But, one thing right now I'm feeling the way I've made this code is not so good. Because I'm duplicating the code at the time of creating namespace for each organization. Can anyone suggest a better way to arrange this code?