-
Notifications
You must be signed in to change notification settings - Fork 486
What socket data exists within redis? #447
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
Comments
Hi! The That being said, you can totally store it in Redis and fetch it upon connection. |
@darrachequesne By storing/fetching it in Redis do you mean I should manually do a "pub/sub" to redis? Or are there built-in function in the socket.io-redis-adapter to do so? Feel free to close this issue after your response. |
You can use the io.on("connection", (socket) => {
socket.data.someAttribute = 42;
});
// then
const sockets = await io.fetchSockets();
console.log(socket.data.someAttribute); // prints 42 Reference: https://socket.io/docs/v4/server-socket-instance/#socketdata This also works with a cluster of Socket.IO servers, when using the Redis adapter (or any compatible adapter). |
Thank you for your reply. Unfortunately, I'm confused now. If io.on("connection", (socket) => {
socket.data.someAttribute = 42;
});
// kill Server1 where the above happened
// then from Server 2
const sockets = await io.fetchSockets();
forEach(s in sockets){
if(s.data.someAttribute === 42) return true; // this is never triggered
} I understand that you can fetchSockets and get their content, but that requires getting the in-memory data from each server, correct? |
Yes, that's right 👍 In your example above, if server1 gets killed, the |
That clears things up. |
I am currently storing data using
socket.data.myCustomData
and am able to retrieve the data properly between servers.However, I'm wondering if this data is stored in redis itself?
By that I mean,
If I kill Server 1, will Server 2 and Server 3 be able to access
mySocket.data
regardless of Server 1 and it's memory/process?The text was updated successfully, but these errors were encountered: