The WebSocket architecture of @elixirphoenix
#myelixirstatus Image
1. The endpoint will start several children, we only focus on server and socket here:

* socket children

pool supervisor + a bunch of dynamic supervisors which will eventually start channel servers

the number of dynamic supervisors is the number of CPU cores of your server Image
* server children

The ranch listener supervisor will start a connection supervisor and an acceptor supervisor.

The acceptors will handle connection creation requests.

A cowboy server process is created after a connection is accepted. ImageImage
2. Socket creation

After getting an HTTP request with a socket upgrade, the cowboy server process will start to initiate middlewares.

The Phoenix cowboy handler is one of the middlewares.

WebSocket protocol flow:
Image
3. After initialization, protocol switch happens, WebSocket protocol will takeover.

It can start to handle TCP messages from clients and Erlang messages from the Phoenix channel servers.
* The websocket_handle callback will be called when clients send messages to the socket.

If there's no related Phoenix channel server, a server process will be started under the dynamic supervisor. ImageImage
* The websocket_info will be called if the channel sends messages to the socket process. ImageImage
4. Broadcast

when a channel server is spawned, the PID is registered under the full topic name in the registry.

If you broadcast a message to a topic, all the pids are retrieved from the registry, and messages will be sent out to either the channel process or socket process. ImageImage
5. Your turn

Now it's time to read more codes and do some experiments. Happy building!
A retweet to the first tweet would be greatly appreciated. 💌

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Daniel

Daniel Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @algolearner

30 Jul
Phoenix.PubSub is probably the most important component in @elixirphoenix @elixirlang .
We will benefit a lot by understanding the internals of Phoenix.PubSub.

#myelixirstatus
Here is the basic structure:
* a :pg server is started with the scope name: Pheonix.PubSub
* a pubsub supervisor supervises a registry and an adapter server
* the adapter server joins a :pg group during initialization
The adapter server is also the PubSub shard, they exist to broker broadcasts across the cluster, and relay those messages to local node subscribers which are recorded in the ETS table in the registry.

This is how it looks like in a two nodes cluster:
Read 6 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(