connected users' list in sails socket - socket.io

i've currently started using sailsJS with angularJs at frontend alognwith socket for realtime communiction.
Sailsjs gives built-in support to websocket through "sails.io.js".On client side after adding this library this code is added to angular's chat controller.
Client side code
io.socket.get('/chat',{token:token},function(users){
console.log(users);
});
chatController's action on sails side is like this.
Server side code
chat: function (req, res) {
console.log(req.isSocket);
//this gives true when called through client.
})
infact very new to sails so i want suggestion that how to maintain connected user's list because m not using redis as storage purpose.adapter is memory.array is not a good idea because it'll vanish when restart a server.m using sails version of 0.11.0.
thanx in advance.

I'm somewhat new but learning fast, these suggestions should get you there unless someone else responds with greatness...
They changed it in 11 but in 10.5 I use sockets.js in config folder and on connect I store the session data in an array with their socket.
I created a service in APIs/service that contains the array and socket associate function.
For v11 you can't do that exactly the same, but you can make your first 'hello' from the client call a function in a controller that calls the associate function.
A couple tips would be don't let the client just tell you who they are, as in don't just take the username from the params but get it from req.session
(This assumes you have user auth setup)
In my case I have
in api/services/Z.js (putting the file here makes it's functions globally accessible)
var socketList = [];
module.exports = {
associateSocket: function(session, socket) { // send in your username(string) socket(object) id(mongoId) and this will push to the socketlist for lookups
sails.log.debug("associate socket called!",socketList.length)
var iHateYou = socketList
//DEBUG
var sList = socketList
var util = require('util')
if (session.authenticated){
var username = session.user.auth.username
var userId = session.user.id
// sails.log.debug("Z: associating new user!",username,userId,socket)
if (username && socket && userId) {
sList[sList.length]= {
username: session.user.auth.username,
socket: socket,
userId: session.user.id,
};
sails.log.debug('push run!!! currentsocketList length',socketList.length)
} else sails.log("Z.associateSocket called with invalid data", username, userId, authId, socket)
}else{sails.log.warn("Z.associateSocket: a socket attempted to associate itself without being logged in")}
},
}
in my config/sockets.js
onConnect: function(session, socket) {
Z.associateSocket(session,socket)
if (session.user && session.user.auth){
sails.log("config/sockets.js: "+session.user.auth.username+" CONNECT! session:",session)
}else sails.log.warn('connect called on socket without an auth, the client thinks it already has a session, so we need to fix this')
// By default, do nothing.
},
Then you can make add some functions to your services file to do lookups based on username and passwords, remove sockets that are disconnecting and the like (I'm using waterlock for my auth at the moment, although debating the switch back to sails-generate-auth)

Remove your onConnect and dicconnect function from config/sockets.js.

Related

What's the difference between socket.to(id) and socket.broadcast.to(id)?

I'm write an application using socket.io.
I'm confused by the official document about socket.broadcast.
From my testing, the below code has the same effect:
socket.to(id).emit('some event')
socket.broadcast.to(id).emit('some event')
What's does broadcast do?
broadcast sets a flag in socket,
Socket.prototype.__defineGetter__('broadcast', function () {
this.flags.broadcast = true;
return this;
});
which tells the manager to omit current socket from broadcasting
Socket.prototype.packet = function (packet) {
if (this.flags.broadcast) {
this.log.debug('broadcasting packet');
this.namespace.in(this.flags.room).except(this.id).packet(packet);
} else {
...
Thus socket.broadcast.to(room) will have the following effect: client that is connected to the socket will not receive the message. Whereas when socket.to(room) all room's clients will receive the message including the one who is connected to socket.
I've just verified this for socket v0.9 but I doubt these mechanics are different for v1.+

ZeroMQ choose recipient

I'm new to ZeroMQ (and to networking in general), and have a question about using ZeroMQ in a setup where multiple clients connect to a single server. My situation is as follows:
--1 server
--multiple clients
--Clients send messages to server: I've already figured out how to do this part.
--Server sends messages to a specific client: This is the part I'm having trouble with. When certain events get handled on the server, the server will need to send a message to a specific client -- not all clients. In other words, the server will need to be able to choose which client to send a given message to.
Right now, this is my server code:
using (NetMQContext ctx = NetMQContext.Create())
{
using (var server = ctx.CreateResponseSocket())
{
server.Bind(#"tcp://127.0.0.1:5555");
while (true)
{
string fromClientMessage = server.ReceiveString();
Console.WriteLine("From Client: {0}", fromClientMessage);
server.Send("ack"); // There is no overload for the 'Send'
method that takes an IP address as an argument!
}
}
}
I have a feeling that the problem is that my design is wrong, and that the ResponseSocket type isn't meant to be used in the way that I want to use it. Since I'm new to this, any advice is very much appreciated!
when using the Response socket you always replying to the client that sent you the message. So the Request-Response socket types together are just simple request response.
To more complicated scenarios you probably want to use Dealer-Router.
With router the first frame of each message is the routing id (the identity of the client that sent you the message)
so your example with router will look like:
using (NetMQContext ctx = NetMQContext.Create())
{
using (var server = ctx.CreateRouterSocket())
{
server.Bind(#"tcp://127.0.0.1:5555");
while (true)
{
byte[] routingId = server.Receive();
string fromClientMessage = server.ReceiveString();
Console.WriteLine("From Client: {0}", fromClientMessage);
server.SendMore(routingId).Send("ack");
}
}
}
I also suggest to read the zeromq guide, it will probably answer most of your questions.

Sailsjs Session access during custom onConnect in socket.io

I'm trying to group all my socket.io connection into groups.
I want 1 group for each sails.js session.
My first goal is authentificate all tabs in a same time.
So I tried to do this with onConnect in config/sockets.js like that :
onConnect: function(session, socket) {
// By default: do nothing
// This is a good place to subscribe a new socket to a room, inform other users that
// someone new has come online, or any other custom socket.io logic
if (typeof session.socket == 'undefined'){
session.socket = [];
}
session.socket.push(socket.id);
session.save();
console.log(session, socket);
},
// This custom onDisconnect function will be run each time a socket disconnects
onDisconnect: function(session, socket) {
// By default: do nothing
// This is a good place to broadcast a disconnect message, or any other custom socket.io logic
if(Array.isArray(session.socket)){
var i = session.socket.indexOf(socket.id);
if(i != -1) {
session.socket.splice(i, 1);
session.save();
}
}
console.log(session, socket);
},
But I realize that session doesn't save my modifications.
I tried a session.save but sailsjs doesn't know req !
Session.set(sessionKey, req.session, function (err) {
I want to access to sails.js sesion but I don't know how to do it.
I tried to search a solution but now, after 6 hours of search I think it's time to requiered some help !
Thanks and sorry for my poor english (I'm french).
There appears to be a bug in the implementation of onConnect and onDisconnect in Sails v0.9.x. You can work around it for now by adding the following line before a call to session.save in those methods:
global.req = {}; global.req.session = session;
then changing session.save() to:
session.save(function(){delete global.req;});
That will provide the missing req var as a global, and then delete the global (for safety) after the session is saved.
Note that this issue only affects sessions in the onConnect and onDisconnect methods; inside of controller code session.save should work fine.
Thanks for pointing this out!

socket io user connection added on every html page refresh?

I have a nodeJS application added with socket io and Express like this:
Server:
var storeEachSocket=[];
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
storeEachSocket.push(socket);
console.log(storeEachSocket.length);
var clients = io.sockets.clients(); //clients is an array
console.log(clients.length);
});
Client:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
</script>
Currently there is only ONE html page served by the server.
I found that, after the socket connection was built between html and server(at this time storeEachSocket.length is 1 and clients.length is 1 ),and if I refresh this html page, both storeEachSocket.length and clients.length would be 2, indicating that there are 2 socket connection between this html and the server.I want to know is this normal? the first socket connection would not die out even if after I create the second socket connection?
And I also want to know, if I intend to track this user(html),what shall I do if there are two socket connections used by the one user?
Refreshing the page doesn't add new connection, it basically removes current connection and creates another new connection. I think you should add a listener to disconnect event and remove the socket in the array that has been disconnected. I also don't believe that io.sockets.clients() would return 2 after refreshing as I've tested it on my machine.
If you want to track users, you need to have a user ID and store it into a key/value pair collection. Then you can restrict a user to create 2 connections by checking the collection in thr authorization handler.
var storeEachSocket = {};
io.set('authorization', function(handshake, accept){
var user = handshake.query;
if(user.id in storeEachSocket)
accept('User already connected', false);
else
accept(null, true);
});
io.on('connection', function(socket){
var user = socket.handshake.query;
storeEachSocket[user.id] = socket;
});
io.on('disconnect', function(socket){
var user = socket.handshake.query;
delete storeEachSocket[user.id]
});

how to uniquely identify users in websocket?

I am confused on how to give each new person that connects to the websocket a unique identification that can only affect his own information on the server. the information on the websocket is broadcast-ed so everyone receives the information from the server. I am using socket io for this.
There is not really enough information to provided a answer tailored to your needs, however here is a common method of sharing session data with sockets using an express.js memory store.
var sio = require('socket.io'),
parseCookies = require('connect').utils.parseSignedCookies
// authorization for sockets
io.set('authorization', function (hsData, accept) {
//get the handshake data for the socket
if(hsData.headers.cookie) {
//parse the cookies from the handshake data
var cookies = parseCookies(cookie.parse(hsData.headers.cookie), "session secret"),
sid = cookies["session path"];
//load your session store and find the user
sessionStore.load(sid, function(err, session) {
hsData.user = session.user;
return accept(null, true);
});
} else {
return accept('No cookie transmitted.', false);
}
});
io.sockets.on('connection', function(socket){
//now the socket will have user data
var user = socket.handshake.user,
});
Take a look at Baloons.IO to look at some great source code where sessions and socket.io live in harmony using a redis store.
Hope this helps!

Resources