Smart real-time with socket.io

I'm currently developing a PHP website and it has a posts+comments system. I also have a socket.io server to have real-time communication through comments to posts (just like facebook). Everything is fine now, except that I send a comment to all connected users. And I'm thinking that when I'll have like 10.000 users and one comments on a post, it sends that comment to all 10.000 users and maybe the connection will crash pretty soon this way.

I think I have to keep the posts a user is viewing. Maybe in PHP, when I show up some posts, I keep in the database their id and it will expire after a period, like an hour. In this case, when somebody comments on a post, I get the users who are seeing that post and send the comment only to them. Is there a cleaner way to accomplish that?

Answers


Socket.io has the concepts of Rooms

You could create a different room for each post and have the clients subscribe only to the relevants rooms (posts).


Need Your Help