Skip to content

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

Closed
y4my4my4m opened this issue Apr 6, 2022 · 6 comments
Closed

What socket data exists within redis? #447

y4my4my4m opened this issue Apr 6, 2022 · 6 comments

Comments

@y4my4my4m
Copy link

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,

  • Server 1 -> mySocket.data
  • Server 2
  • Server 3

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?

@darrachequesne
Copy link
Member

Hi! The socket.data is not persisted to Redis, it only exists in the memory of the 1st server.

That being said, you can totally store it in Redis and fetch it upon connection.

@y4my4my4m
Copy link
Author

@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.
Thank you for everything :)

@darrachequesne
Copy link
Member

You can use the fetchSockets() method:

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).

@y4my4my4m
Copy link
Author

y4my4my4m commented Apr 11, 2022

@darrachequesne

Thank you for your reply.

Unfortunately, I'm confused now.
Didn't you just say the data doesn't persist in redis?

If socket.data.someattribute = 42 only exists in memory on Server1, after killing server1 wouldnt we get something like this:

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?
Or do you mean that using fetchSockets will make the data persistent in redis?

@darrachequesne
Copy link
Member

but that requires getting the in-memory data from each server

Yes, that's right 👍

In your example above, if server1 gets killed, the data is lost. Sorry for the confusion.

@y4my4my4m
Copy link
Author

That clears things up.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants