rlefevre

rlefevre

Hi,

I’m developing a SPA with a phoenix backend that uses channels for real-time updates to all clients.

A client typically creates/updates/deletes some objects and the modifications are then broadcasted.
I see mainly 2 ways to do it:

  1. Perform CRUD methods using HTTP requests and broadcast modifications to all clients using channels (maybe excluding the one that did the request if it got everything needed in the response).

  2. Use HTTP only to load the SPA, then use websocket channels for all operations.

I guess that there is no clear answer to which one is the best. Using HTTP requests, we get a quite robust request/reply mechanism with HTTP status codes. Using channels, performance might be better, but we have to use some kind of request/reply (edit: most likely using channels ref) and we lose the HTTP status codes (which might not be precise enough anyway).

From your experience, what are the pros and cons of each method, particularly in regard to performance, race conditions and synchronization issues ?

Thank you very much.

Showing Posts 1 to 10

kokolegorille

kokolegorille

You will use websocket/channels anyway for feedback.

So, it is more like how do You want to CRUD your data, through an API, AND/OR through websocket. This is not exclusive.

The API is more compatible, because You can use any http client.

But if You don’t need to expose an API, You can go websocket/channel all the way down.

StefanHoutzager

StefanHoutzager

Some previous threas that might contain interesting info in this forum (search for “SPA”):

StefanHoutzager

StefanHoutzager

This might be of interest also:

maybe the time has come to treat WebSockets as first-class citizens and to start
introducing RESTful HTTP verbs to WebSockets (as naming conventions for 
events). This is very new territory, but there are some important benefits to
using REST over WebSockets:
michalmuskala

michalmuskala

To be honest, I would probably broadcast only ids over websockets (for realtime stuff) and then retrieved the full data using regular HTTP API. Using HTTP has a lot of benefits that are often overlooked - extremely easy reporting through a multitude of tools, easy caching, easy rate-limitting, and more.
If the data is small, then it might make sense to send it all through websockets, but for big payloads the overhead of HTTP is easily outweight by the other things, primarily caching - even more so for public resources.

rlefevre

rlefevre OP

Thank you for your answers. As an HTTP API is not absolutely required in my case, I have some trouble to make up my mind and needed more input, you are very helpful in this regard.

@kokolegorille
As said, an HTTP RESTfull API is not required so far, but I would like to avoid having both an HTTP API and a websocket API with redundant features. Hence my hesitation. The clients compatibility is a good point.

@StefanHoutzager
Thank you for the links:

  • json-pure is interesting as this would allow me for example to use a websocket only API at first but still being able to add support for an HTTP endpoint later if required, even if caching is mostly impossible in this case (and realtime feedback limited).

  • Why I No Longer Use MVC Frameworks article from Jean-Jacques Dubray raises some interesting considerations about GraphQL (that I am reluctant to use at this point). Also I may take his SAM pattern into consideration as I use elm for the frontend and it seems easy to implement using The Elm Architecture, even if it does not enforce it by default (as stated in this comment).

@michalmuskala
Thanks, you raise some strong points there. I am worried about performance as the clients have specific permissions that must not allow them to receive the same messages (and client filtering would be considered as a security issue). Using channels, this will require to use intercept on all messages or using per-client channels, and using HTTP, this will make intermediary caching mostly pointless (almost no public resources). But browser caching could still be possible and should indeed be taken into consideration, as well as tooling.

StefanHoutzager

StefanHoutzager

Concerning what mr. Dubray says I would carefully check what he writes. He likes to ride high on his horses and produces lots of quite uncomprehensible prose, in my opinion.

rlefevre

rlefevre OP

Well, at least it seems to me that his considerations about APIs being too much dedicated to frontend views and therefore unstable because of it make sense, even if at the end, practical “what works” will win for me. And I actually use the functional approach to adapt the model data for views (which is quite natural in elm).

Also again in elm, it is tempting to include view states in the model, which can become confusing. Having separated “view states” (even if it is one field of the model record) is an approach. Another one is to use “sub-models” as in the elm-spa-example from Richard Feldman, but it is not always wanted to loose a SPA “page” state when changing “page”. Anyway this might drift too much away from the intial intention of the post.

rlefevre

rlefevre OP

@StefanHoutzager I forgot but I also did not take enough into consideration so far the point in REST over WebSockets instead of HTTP about the corporate environments compatibility with websockets.

I will move to wss:// (it seems that using SSL in phoenix should make websockets also use SSL) and hopefully I won’t meet a customer using proxies with fake root certificates (I actually worked some years ago in a company using this, urgh…). Thanks for the link.

l00ker

l00ker

I’m (eventually) going for the same idea. I’m just not quite comfortable with my current Elixir skills at this point to get there, but I will get there.

Have a look at this Node.js project Feathers for some ideas. It uses websockets for it’s realtime updates. This is where I was before coming to Elixir and felt that something akin to Feathers could be done much better using Phoenix/Elixir/Erlang/OTP.

Just food for thought :slight_smile:

COLABORATI

COLABORATI

Anything happaning on this front? I am on the same track - feathersjs is really very interesting, but with Elixir backend it would be the stack for the next ten years.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews