brendan

brendan

I would like to discuss how phoenix can be improved regarding websocket connection validation with Phoenix Channels.

The issue:

Connecting to a websocket generally involves some kind of authentication/authorization and it needs to be possible to close the websocket connection if this fails.
Additionally, in order to have different client behaviour based on the reason that it was closed, we need to be able to pass at least a reason code, if not a full message on or before closing the connection.
E.g. if the reason is that the auth token is invalid, then the client should not attempt to reconnect until it has acquired a new token.
This should be flexible so that application developers can implement their custom requirements.
However currently it is not possible to both close the connection and provide a reason that is accessible to the client (at least for the browser websocket api).

Existing discussions

There are existing discussions regarding this that have been pretty dismissive of this issue:

Workarounds

The proposed workaround (and the one we currently use) is to require the client to join some special auth channel after connecting, before connecting to any other channels. As you can imagine, this results in multiple issues:

  1. All channels need to perform some kind of validation on join. This isn’t that bad since it can just delegate to a module which checks the socket.assigns.
  2. You can only do auth after the client has decided to join a channel. There is no escape hatch to send a message to the client before they have joined any channels. This makes the server vulnerable to DOS attacks since the client can just open websocket connections without joining any channels and the server will just keep these idle connections running because auth isn’t done on websocket connect.

This leaves us stuck between 2 places.

  1. Either close the websocket connection without a reason, which means the client will always continue attempting to reconnect, even if their token etc is invalid. This leaves us with misbehaving clients resulting in us getting kinda DOS’d by our own clients.
  2. Make it very easy for a bad actor to perform a DOS attack on the server.

Solution

I understand that phoenix channels are transport agnostic and that the correct behaviour cannot be implemented in the channels, but we need to figure out a way to handle this correctly in specific transports.

I think the most flexible solution would be providing some way to send a message from the server to the client without needing the client to join any channel, but if this is not realistically possible given the channel protocol, then we should at least provide a way to send a close frame on the socket connect. Note however that close frames are quite limited. This could be done by instead of rejecting the http upgrade on a connect error, we successfully upgrade the connection and then immediately sending a close frame. Having looked at the phoenix source, this seems like a minor change, except for the fact that cowboy doesn’t seem to support sending custom close frames currently, so we would have to add that support to cowboy first.

I hope that we can have a fruitful discussion here and find a good solution to a problem that I believe a lot of developers have with websocket/phoenix channels currently.

Showing Posts 1 to 1

brendan

brendan OP

I have created a draft PR that solves this issue at [Socket] send connect error to client and close by BrendanBall · Pull Request #4886 · phoenixframework/phoenix · GitHub.
Previously if socket connect handler returns an error
then websocket upgrade would be rejected.
Browser websocket api doesn’t support getting the status code on upgrade
failure.
This instead successfully upgrades to websocket then sends the error as
the first message and immediately closes the socket.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews