-
Notifications
You must be signed in to change notification settings - Fork 486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to construct Socket object? #137
Comments
Hi! May I ask why you'd want to persist the socket object itself? Don't you mean the data attached to it instead? |
Well, actually not. Imagine the following scenario:
Because I can have only socket id at the moment, I don't know how to get actual Socket object from it to add more events at certain moments. |
Hi, I have a related use case. After making this call, I 'd like to send a message to one of the sockets after filtering the list on some socket attributes. Without the socket.io redis adapter, it is possible to something like this: func getSocketForEmail(email) {
var ns = global.io.of(namespace || "/");
for (var id in ns.connected) {
var currentSocket = ns.connected[id]
if (currentSocket.email.equals(email)) {
return currentSocket
}
}
} |
Actually it's not, if you're using clustered mode. |
@overflowz right, that is what I 'm trying to confirm. The sample code I gave was without the cluster and the redis adapter (i.e a single node case). I don't really need the socket object, I just need the ability to query on socket attributes and send a message remotely to that socket (kind of like https://github.com/socketio/socket.io-emitter). I wonder if http://socketcluster.io/#!/ has a feature like this. |
@lakamsani I've rewritten whole logic in my application and made it in a different way. Now you can use customRequest method, check the README page. Regards. |
@overflowz I noticed that but wasn't sure if it can be used to solve this use case. Based on your comment then, will something like this work to address my use case? // on sender
io.adapter.customRequest('<some json>', function(err, replies){
console.log(replies); // an array ['response1','response2', ...] with one element per node
});
// on each of the other nodes
io.adapter.customHook = function (data, cb) {
// if there is a local socket matching attributes in data param then send the message and ack via cb
cb("I did it")
OR
// no matching local socket.
cb("I do not have that socket")
} Is it possible to pass arbitrary JSON to the first parameter of io.adapter.customRequest? thanks. |
@lakamsani I actually haven't tested that method by myself, but according to latest socket.io version, it should be io.sockets.adapter (instead of io.adapter) and yes, it should work fine (I think). |
@overflowz this seems to work in our preliminary testing with 3.1.0 version of socket.io-redis. One issue we found is that the second arg of customHook i.e. 'cb' is undefined. We don't really need it for our use case. We are doing more testing but looks like it it will save from implementing this ourselves either using redis pub/sub directly or something like Amazon SQS. |
@lakamsani that's pretty weird! Can you produce a snippet for that please? Thanks! |
@lakamsani hi! I guess that's because #181 is not released yet. |
@darrachequesne thanks for that info. We will look for it in the next release. @overflowz sample code is quite simple actually. Write something like as in the doc and try to invoke the callback.. io.sockets.adapter.customHook = function (data,cb) {
cb("good"); // this will fail in 3.1.0 as cb is undefined
} |
@lakamsani yeah, that's because latest version was not released though. I was tired a lot when I replied it :) |
Released as 4.0.0 |
Hi, I'm trying to make a game and whenever I use a socket to do things, I'm saving every kind of information in key/value store. Problem is that, I can't save Socket object itself and I'm curious what's the correct way to construct Socket object? What information do I need?
Regards.
The text was updated successfully, but these errors were encountered: