Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

websocket communication between clients in distributed system

I'm trying to build instant messaging app. Clients will not only send messages but also often send audios. And I've decided to use websocket connection to communicate with clients. It is fast and allows to send binary data.

The main idea is to receive from client1 message and notify about it client2. But here's the thing. My app will be running on GAE. And what if client1's socket is opened on server1 and client2's is opened on server2. This servers don't know about each others clients.

I have one idea how to solve it, but I am sure it is shitty way. I am going to use some sort of communication between servers(for example JMS or open another websocket connection between servers, doesn't matter right now).
But it surely will lead to a disaster. I can't even imagine how often those servers will speak to each other. For each message server1 should notify server2, server2 should notify client2. But things become even worse when serverN comes into play.

Another way I see this to work is Firebase. But it restricts message size to 4KB. So I can't send audios via it. As a solution I can notify client about new audio and he goes to my server for it.

Hope I clearly explained the problem. Does anyone know how to solve it? Or maybe there are another ways to build such apps?

like image 265
Vladislav Gutov Avatar asked Oct 31 '22 00:10

Vladislav Gutov


1 Answers

If you are building a messaging cluster and expect communicating clients to connect to different instances of the server then server-server communication is inevitable. Usually it's not a problem though.

  • First, if you don't use any load balancing your clients will connect to the same server 50% of time on average (in case of 2 servers).
  • Second, intra-datacenter links are fast and free in all known public clouds.
  • Third, you can often do something smart on the frontend to make sure two likely to communicate clients connect to the same server. For instance direct all clients from the same country to the same server using DNS load balancing.

The second part of the question is about passing large media files. It's a common best practice to send it out of band - store on the server and only pass the reference to it. Like someone suggested in the comment, save the audio on the server and just send a message like "audio is available, fetch it from here ...". You don't need to poll the server for that. Just fetch it once when the receiving client requests it.

In general, it seems like you are trying to reinvent the wheel. Just use something off the shelf.

like image 130
Gene S Avatar answered Nov 15 '22 08:11

Gene S