Last active
August 7, 2019 01:39
-
-
Save otnansirk/d02c28c88b6e36c59c8aaa2d3ee4db70 to your computer and use it in GitHub Desktop.
socket.io with namespace, multiple socket.id and disconnect socket.id
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 app = require('express')(); | |
var http = require('http'); | |
var httpServer = http.createServer(app); | |
var io = require('socket.io')(httpServer); | |
/** | |
* get unique data | |
*/ | |
function onlyUnique(value, index, self) { | |
return self.indexOf(value) === index; | |
} | |
/** | |
* emit function | |
*/ | |
function emitTo(data, emitTo){ | |
recipientID = users[data.toId]; | |
if (recipientID != undefined) { | |
for (var i = 0; i < recipientID.length; i++) { | |
ioPrivate.to(recipientID[i]).emit(emitTo, data); | |
} | |
} | |
} | |
/** | |
* Notif test | |
*/ | |
app.get('/', function (req, res) { | |
res.sendFile(__dirname + '/index.html'); | |
}); | |
/** | |
* private notif | |
*/ | |
var ioPrivate = io.of('/private'); | |
users = {}; | |
admins = {}; | |
ioPrivate.on('connection', function(socketPrivate){ | |
console.log('private message') | |
/** | |
* register user to socket connect | |
* @data { | |
* userId : use id that is logged in (required) | |
* } | |
*/ | |
socketPrivate.on('register-user', function(data){ | |
if(users[data.userId] != undefined){ | |
socketIds = users[data.userId]; | |
socketIds.push(socketPrivate.id); | |
users[data.userId] = socketIds.filter(onlyUnique); | |
}else{ | |
users[data.userId] = [socketPrivate.id]; | |
} | |
socketPrivate.userId = data.userId | |
socketPrivate.userSocketId = socketPrivate.id | |
console.log('user registered'); | |
console.log(users); | |
}) | |
/** | |
* register user to socket connect | |
* @data { | |
* userId : use id that is logged in (required) | |
* } | |
*/ | |
socketPrivate.on('register-admin', function(data){ | |
if(admins[data.companyId] != undefined){ | |
socketIds = admins[data.companyId]; | |
socketIds.push(socketPrivate.id); | |
admins[data.companyId] = socketIds.filter(onlyUnique); | |
}else{ | |
admins[data.companyId] = [socketPrivate.id]; | |
} | |
socketPrivate.companyId = data.companyId | |
socketPrivate.adminSocketId = socketPrivate.id | |
console.log('admin registered'); | |
console.log(admins); | |
}) | |
/** | |
* recieve message | |
* @data { | |
* userId : use id that is logged in, | |
* message : socket.id, | |
* toId : the destination user id to receive the message (required) | |
* } | |
*/ | |
socketPrivate.on('room', function(data){ | |
console.log('rom'); | |
console.log(data); | |
emitTo(data, 'new_private_message'); | |
}) | |
/** | |
* recieve notif | |
* @data { | |
* userId : use id that is logged in, | |
* message : socket.id, | |
* toId : the destination user id to receive the message (required) | |
* } | |
*/ | |
socketPrivate.on('private_notif', function(data){ | |
console.log('private_notif'); | |
console.log(data); | |
emitTo(data, 'new_private_notif'); | |
}) | |
/** | |
* recieve notif for admin | |
* @data { | |
* companyId : use id that is logged in, | |
* message : socket.id, | |
* toId : the destination user id to receive the message (required) | |
* } | |
*/ | |
socketPrivate.on('admin_notif', function(data){ | |
console.log('admin_notif'); | |
console.log(data); | |
adminSocketID = admins[data.companyId]; | |
if (adminSocketID != undefined) { | |
for (var i = 0; i < adminSocketID.length; i++) { | |
ioPrivate.to(adminSocketID[i]).emit('new_admin_notif', data); | |
} | |
} | |
}) | |
/** | |
* remove socket.id when disconnect | |
*/ | |
socketPrivate.on('disconnect', function(data){ | |
let cloneUsers = Object.assign({}, users); | |
cloneUsers[socketPrivate.userId] = (cloneUsers[socketPrivate.userId] || []).filter((item)=> item != socketPrivate.userSocketId); | |
users = cloneUsers; | |
let cloneAdmins = Object.assign({}, admins); | |
cloneAdmins[socketPrivate.companyId] = (cloneAdmins[socketPrivate.companyId] || []).filter((item)=> item != socketPrivate.adminSocketId); | |
admins = cloneAdmins; | |
}) | |
}); | |
httpServer.listen(3000, function () { | |
console.log('listening on *:3000') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment