jpbecotte

jpbecotte

How to handle states sync in an «offline first» approach?

Hello all,

The context is simple: When a user joins the main lobby channel of my app, it receives its state. Then, the user interacts over the front-end UI, where its state is mutated.

So I see two paths here:

Path #1: When a mutation occurs, send a request thru the socket (or over API), then wait for the server to process the mutation.

This is the old request-response mechanism and it may suit a lot of needs. But it has caveats: the front-end must be constantly connected over an internet connection. Plus, there might be a delay between the mutation occurs and its acknowledgment from the server.

Path #2: When a mutation occurs, simply mutate local state. Syncing is made in background with a presence mechanism. If a client goes offline for any reason, then its state gets synced next time it reconnects.

My question is: what is the best way to implement path 2?

I’ve looked at Phoenix.Tracker, which «use a heartbeat protocol and CRDT to replicate presence information across a cluster in an eventually consistent, conflict-free manner» and it looked promising. Trackers work with presence metadata. But I read in the Phoenix.Presence doc: « Presence metadata should be minimized and used to store small, ephemeral state, such as a user’s “online” or “away” status.».

So I am confused. Is Phoenix.Tracker the best way to implement this or there is a better way of doing it?

Thanks in advances!

JP

Most Liked

LostKobrakai

LostKobrakai

Offline first is just a more fancy way to say distributed state. So all the solutions / tradeoffs and issues of the latter apply here. Phoenix Tracker for example uses CRDTs to distribute the presence of something/someone in a channel. The metadata is just a bag of data assigned to that presence, which is not merged in any way as far as I know. It’s simply replaced.

So you’d need to check if you problem can be solved using the particular solution, but there are many other ways for merging potentially conflicting state all with their own set of tradeoffs.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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

We're in Beta

About us Mission Statement