railsmechanic

railsmechanic

Hi alchemists!

Currently we’re using server sent events (SSE/Eventsource) to notify client applications (written in Go) about model changes/updates in a web application. Each client application consumes the stream which is provided by Rails and its “Live::SSE” feature. As our company plans to (slowly) migrate existing Rails Apps to Phoenix, we also want to replace this Rails API with Phoenix.

While I’ve found many examples of using Phoenix channels, I haven’t found useful documentations or examples for building the server sent events functionality. Our Rails application currently uses Redis PUB/SUB to communicate model changes/updates. Every time a model changes, an event is published to Redis. This event is then consumed on the Rails side and “transformed” to server sent events and streamed to the connected client applications.

We’re not bound to Redis as an internal message dispatcher. I think Phoenix offers a better approach with using channels internally. The only thing we want to keep is the use of server sent events. How would an implementation for server sent events look with Phoenix? Can you provide some (links with) examples?

Many thanks.

Showing Posts 1 to 10

OvermindDL1

OvermindDL1

As far as I’ve seen yet there is no built-in way to do SSE in Phoenix, and as the Phoenix channel API covers that use-case and more via websocket/longpolling, most have not seen a need.

However, there is a good use-case for SSE and it still can be done in Phoenix/Plug.

First, you will want to add the mimetype of "text/event-stream" as an acceptable mimetype at whatever router paths you want.

Second, you will need to send data via normal HTTP chunked encoding if I remember right. I know how to do this in Cowboy, and I know Phoenix/Plug does it via longpolling channels, but I’ve not done it in Phoenix/Plug itself so maybe @josevalim can elaborate on how it is done as a quick googling failed me? ^.^

And the above might not even be necessary if Phoenix Channels already have a built-in way to emulate Server-Side Events, I’ve not checked yet. :slight_smile:

Edit: Just as I send this I find a plug way to send chunked data, apparently it is just the chunk/2 call: A super simple Elixir server for sending Server Sent Events to the browser. · GitHub ^.^

Some kind of one-way channel interface would be a better way if it exists already though. :slight_smile:

railsmechanic

railsmechanic OP

We’ve actually considered one-way channels, but we haven’t found a suitable client for Golang (and other programming languages). I’ve not looked at it more deeply, but can Phoenix channels be used with common websocket clients?

hubertlepicki

hubertlepicki

yes, it is doable. Check out the source code for the official phoenix.js client library for reference, here’s direct link to the class/object/whatever responsible for establishing and handling websocket connection:

https://github.com/phoenixframework/phoenix/blob/master/priv/static/phoenix.js#L649

Basically you connect to a websocket by URL and listen to onMessage, where you get a JSON payload to deserialize and interpret. For 1-way connection that could be all you need.

OvermindDL1

OvermindDL1

As @hubertlepicki put it, yes it is, and that is the general preferred way as it uses websockets for speed if possible, but can fall back to longpolling (if you enable it in your socket on elixir), which longpolling is what is used for SSE anyway, so it covers all use-cases, just might be more powerful than what you need, but it is so so simple to use too and I’d highly recommend it, plus bidirectional channels can randomly be very useful. :slight_smile:

Crowdhailer

Crowdhailer

Creator of Raxx

I have written a small library for serializing and parsing server sent events. server_sent_event | Hex

@railsmechanic have you stayed using SSE’s? I would be interested in other peoples reason for using them, over websockets etc. and experiences using them.

Crowdhailer

Crowdhailer

Creator of Raxx

Another quick comment.

I am considering using server sent events as a method for streaming data from the client to a server. would be interested if anyone here has ever gone down that path.

railsmechanic

railsmechanic OP

SSE, as far as I know, is not intended for that kind of use case, as the client needs to expose a server port where your “real” server can connect to, in order to receive data. For that kind of information exchange I think Websockets are the way to go, because they’re bi-directional out of the box.

Crowdhailer

Crowdhailer

Creator of Raxx

I think we may be at crossed wires. The server is not connecting to the client, no need for any ports. The request (sent from client to server) is deliver chunked like a file upload and the body content is serialized as SSE’s (though at this point the fact they are called SSE’s is not great

railsmechanic

railsmechanic OP

OK, got it… :grin:

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews