ademenev

ademenev

LiveView streams cannot survive reconnect?

Ok, I admit, the title is clickbait.

There are some examples about using streams, including LiveView documentation. They all look beautiful and work nicely. Well, until the socket reconnects for whatever reason. So, for example, on mount we load a list of latest chat messages, and as we scroll up, we load older messages. And when the socket reconnects, we are back to list of latest messages, and no historical messages loaded while scrolling.

I was not able to overcome this, so I am wondering, is it that streams cannot survive reconnect or that’s just skill issue?

Most Liked

josevalim

josevalim

Creator of Elixir

It is obviously up to you to implement the UX the user expects. :slight_smile: Especially because the root cause of the problem is not caused by streams.

The basic use case of streams is to not send the data on every update. An example scenario without streams is: “I send all of the data on mount and, once a new item is added to the collection, I resend the whole collection”.

That’s obviously expensive, so streams allow you slightly change it to: “I send all of the data on mount and, once a new item is added, I only send that item”. In this example, if you lose the connection, then you will get back to the same page, because you still send everything on mount. So streams do not naturally exhibit this issue.

The root issue is that you are not sending the whole data on mount (and indeed, you should not). For example, imagine that instead of streams, you always send the last 100 messages, on mount and every update. That’s not efficient and it would still have the same issue after the 100 messages window.

Of course, the solution depends on what you want to achieve. The simplest is to store an index in the client and fetch all messages after that index on mount. But you could try to be smarter and cache it locally. Do you want to persist across reconnects? Or even if the user refreshes the page? How much data are you willing to refresh? Also keep in mind that, even if you store in the client, messages can be deleted and updated from the server.

A hammer will help you build a house, but it won’t build one for you. :slight_smile:

LostKobrakai

LostKobrakai

You’d retain the current page (e.g. in the url), so that the reconnect wouldn’t load latest messages anymore, but the current section the user was looking at. You’d loose the other messages “off screen” instead, which would be loaded again on further scrolling.

Stream do however not attempt to retain any data across remounts. If you need caching like that you want to look at other options – like phx-update="ignore" and some client side code to deal with updates.

ademenev

ademenev

Well, if we stay on chat example, that’s not the UX a user would expect.

Last Post!

Hermanverschooten

Hermanverschooten

Thanks for the insight and the reference to storing an index, I hadn’t thought of that (yet).

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement