zizheng

zizheng

Distributed WebSocket & WebRTC chat server

Hi all, I have a design question about implementing a distributed chat server. I’ll start by explaning my use case, and why I find it difficult.

Basically I’m trying to build a simple Discord for our special need. For those who haven’t used Discord before, Discord is a text & voice chat app which uses WebSocket for text chat and WebRTC for voice chat. Users can join a channel (i.e. a chat room) and start talking to each other.

In order for the users in a channel to hear each other, the server needs to maintain WebRTC connections to all the users in the channel, and forward the WebRTC media packets from a user to all the other users in the channel. For this reason, I imagine it’d be necessary (at least much more efficient) to have all the users in a channel connected to the same server, so that the voice session state (e.g. WebRTC connections) lives in a single process.

But I can’t seem to see an easy way to gaurantee that users in the same channel are connected to the same server when we are running more than one servers. I considered using stick sessions, but after some research it turned out only vanilla HAProxy supports sticky sessions based on URL params, and all other load balancers/reverse proxies/etc. only supports sticky sessions based on HTTP headers or cookies. The problem is, WebSocket requests can’t have custom headers.

I’m curious about how Phoenix Channels are implemented. Is it gauranteed that all connections to a topic is to the same server? If not, how does Phoenix solve the problem of having to forward messages of one user on a topic to the other users on the topic? Does it need to forward the messages first between different servers?

Most Liked

NobbZ

NobbZ

No.

The receiving server will forward the message to all other servers. Each server will check on its own whether or not it has someone registered to that topic or not and drop or distribute the message accordingly.

Of course…

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement