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.
-
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 -
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. -
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. -
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
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 6 of 6 Posts
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
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
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_atand you can get that.Is there a reason that the above won’t work?
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
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.
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
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
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.