Here’s how I implemented it: I use Redis for server-to-server communication. Each client is assigned a unique hub code that associates them with a specific hub upon connection. When a message is sent, the sender checks if the recipient is on the same hub. If they are, the message is delivered directly. If not, Redis is queried to determine the recipient's hub code.
Each hub also listens to a dedicated channel. If the recipient (User B) is on a different hub or server, but their hub code matches a hub stored in Redis, the message is routed to that server. If no match is found, it means the recipient is offline. In that case, the message is marked as unread and stored in both Redis and the database.
I hope this explanation helps!