Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js tcp socket server on multiple machines

Tags:

node.js

redis

I have a node.js tcp server that is used as a backend to an iPhone chat client. Since my implementation includes private group chats I store a list of users and what chat room they belong to in memory in order to route messages appropriately. This all works for fine assuming my chat server will always be on one machine, but when/if I need to scale horizontally I need a good way of broadcasting messages to clients that connect to different servers. I don't want to start doing inter-process communication between node servers and would prefer sharing state with redis.

I have a few ideas but I'm wondering if anyone has a good solution for this? To be clear here is an example:

User 1 connects to server 1 on room X, user 2 connects to server 2 on room X. User 1 sends a message, I need this to be passed to user 2, but since I am using an in memory data structure the servers don't share state. I want my node servers to remain as dumb as possible so I can just add/remove to the needs of my system.

Thanks :)

like image 825
Emmanuel P Avatar asked Sep 25 '11 22:09

Emmanuel P


1 Answers

You could use a messaging layer (using something like pub/sub) that spans the processes:

                             Message Queue
-------------------------------------------------------------------------------
            |                                     |
         ServerA                               ServerB
         -------                               -------
Room 1: User1, User2                  Room 1: User3, User5
Room 2: User4, User7, User11          Room 2: User6, User8
Room 3: User9, User13                 Room 3: User10, User12, User14

Let's say User1 sends a chat message. ServerA sends a message on the message queue that says "User1 in Room 1 said something" (along with whatever they said). Each of your other server processes listens for such events, so, in this example, ServerB will see that it needs to distribute the message from User1 to all users in its own Room 1. You can scale to many processes in this way--each new process just needs to make sure they listen to appropriate messages on the queue.

Redis has pub/sub functionality that you may be able to use for this if you're already using Redis. Additionaly, there are other third-party tools for this kind of thing, like ZeroMQ; see also this question.

like image 107
Michelle Tilley Avatar answered Sep 29 '22 23:09

Michelle Tilley