jaybe78

jaybe78

Hello,

I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).

The difficulty I’m facing is detecting gaps.

A user can be offline, lose connection, or messages can be dropped.
When user comes back from being offline, you need to return the latest data, and it needs to be in synced with the history stored in his phone.
when scrolling, it should also picks up gaps between latest messages and existing history.

All of that I think requires that each message when created has a global sequence per conversation.
I think that’s what “what’s app” does actually.

So my initial goal was to be able to get generate a global sequence per conversation which would be the source of truth, so that I can easily check arrived messages in my front end and if necessary query missing data from dynamob or the cache.

  1. I first tried to use dynamodb to get the message sequence with UPDATE_ITEM.
    But that would require one update(for the sequence) and one put(for the message).
    So one could your work and not the other… it’s not ideal

  2. Then the other option is to use a transaction, but the transaction cannot return the sequence after creation.
    It’s also more expensive and less scalable.

  3. Another one probably naive is to use ETS to increment a local sequence.
    All process sending messages would go through that to make sure the sequence is atomic across the node.
    When node goes down ets counter is lost and you’d get the latest sequence with a consistent read from dynamo, but you could have race condition.

  4. Increment sequence with ets and then PUT item with message and index. When the node goes down, the first time consistent read to get the latest sequence

Any idea how tackle this ?

Cheers

Showing Posts 1 to 6

allenwyma

allenwyma

What I did was when I reconnect, I’ll send the last received message ID and server will send back any missing messages so it can be caught up.

I did this over web socket on the client device. On the client I have a storage of some kind.

jaybe78

jaybe78 OP

Hello

I don’t think you understood my question.
I’m asking how to generate and maintain a reliable per-conversation ordering/sequence so I can detect gaps and reconcile chat history

allenwyma

allenwyma

No, I read it quite clearly.

As I said, upon reconnect: i send the last message ID per room, and ask for any messages following that, and the code will query any messages that came after that and send them back to the user. Just order by inserted_at and you can get that.

Is there a reason that the above won’t work?

allenwyma

allenwyma

Ah, i saw you mentioned dynamodb: that i haven’t worked a lot with:

but if you want to copy whatsapp: they are using jabberd: you’re welcome to copy that, of course. I believe they also keep an in-memory of each message and when you reconnect, they check that queue and send out the messages.

There’s quite a few ways to accomplish this.

jaybe78

jaybe78 OP

As I said, upon reconnect: i send the last message ID per room, and ask for any messages

Yes I suppose you do, but that does not say what is your messageID and how do you maintain order and consistency ?
That’s the whole point of my question. :slight_smile:

Is that a date ? a monotonous sequence ?

  • if it’s a date, how do you maintain consistency ? date would not allow you to detect gaps between 2 messages. Yes they would be in the right order, but that’s the only thing you can get from it.

  • if it’s a sequence, how do you maintain order of that sequence across your nodes ?
    Hence the suggestions I offered above.

Getting the “last message id”, would get you the latest messages received, but you could still have “holes” in there, and misalignments with the messages you have in your history.

No I don’t plan on redeveloping whats app lol, as you said, it would take some time, and to be honest, I’m not sure I have the knowledge and expertise to achieve such a thing on my own :slight_smile:

Though I guess there’s probably ways to develop an online chat that respect consistency across disconnect and netsplit :). It will not be as scalable as what’s app but it would respect those criterias.

Anyway cheers :slight_smile:

LostKobrakai

LostKobrakai

I’d consider if you can split up the parts you want to solve here. One is stable ordering. For that you can look at lamport clocks or vector clocks. The other is guaranteed delivery of messages, which is usually solved with acks and retries. You could do that on the individual message level or on chunks of message, whatever scale makes sense. And if you’re worried about detecting bugs (as in messages lost after acknowledgement) you could in intervals create e.g. checksums on sets of messages and compare them with the server.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews