jaybe78

jaybe78 OP

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

First 6 of 6 Posts Switch mode

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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement