When to Write a New Socket

Learn when to write a new Socket or use the same topic.

We'll cover the following

Writing a new Socket

We’ve written two Sockets so far, UserSocket and AuthSocket, and we wrote Channels for each of them. This raises the question of when we should create a new Socket versus using the same topic and adding a new Channel. We’ll make this decision based primarily on the authentication needs of our application. There are also performance costs to adding new Sockets; let’s look at these costs first.

Each connected Socket adds one connection to the server, but each connected Channel adds zero new connections. Channels do take up a slight amount of memory and CPU because there is a process associated with each. Still, we can consider Channels nearly free because processes are cheap in Elixir. Sockets are a bit more expensive due to network connections and the heartbeat process.

Each Socket must maintain a heartbeat to the server. If four Channels are open on a single Socket connection, that means that there is one heartbeat process occurring:

Get hands-on with 1200+ tech skills courses.