Last active
August 29, 2015 14:02
-
-
Save RWOverdijk/d4d552be3e27221c974e to your computer and use it in GitHub Desktop.
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 connections = {} | |
, userService; | |
userService = { | |
/** | |
* Update the ID of the socket connection for a user. | |
* | |
* @param {String} userId | |
* @param {String} socketId | |
*/ | |
updateSocketId: function (userId, socketId) { | |
sails.models['user'].update(userId, {socketId: socketId}).exec(function (error, updated) { | |
if (error) { | |
// @todo decide what to do with errors. | |
} | |
if (null === socketId) { | |
delete connections[userId]; | |
} else { | |
connections[userId] = socketId; | |
} | |
}); | |
}, | |
/** | |
* Connect a client and store his/her socket ID. | |
* | |
* @param {String} userId | |
* @param {String} socket | |
*/ | |
connect: function (userId, socket) { | |
var socketId = sails.sockets.id(socket); | |
this.updateSocketId(userId, socketId); | |
}, | |
/** | |
* Disconnect a client and remove his/her socket ID. | |
* | |
* @param {String} userId | |
*/ | |
disconnect: function (userId) { | |
this.updateSocketId(userId, null); | |
}, | |
/** | |
* Send an event to a specific user. | |
* | |
* @param {String} userId | |
* @param {String} event | |
* @param {*} data | |
*/ | |
emitTo: function (userId, event, data) { | |
function emit() { | |
return sails.sockets.emit(connections[userId], event, data); | |
} | |
if (connections[userId]) { | |
return emit(); | |
} | |
// Performance is key. No need to look up the user if the application isn't scaled anyway. | |
if (!sails.config.scaling.scaled) { | |
return; | |
} | |
// Fetch the user and find the socket id. | |
sails.models['user'].findOne(userId).exec(function (error, data) { | |
if (error) { | |
// @todo decide what to do with errors | |
} | |
if (null === data.socketId) { | |
return; | |
} | |
connections[userId] = data.socketId; | |
emit(); | |
}); | |
} | |
}; | |
module.exports = userService; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment