Using Elm with a javascript CRDT implementation

Hi,

Has anyone considered using Elm together with one of the major CRDT implementation such as https://automerge.org/ or https://yjs.dev/ ?

At first glance it would require defining ports, encoder/decoders and so on… which seems quite heavy. On the other hand one would not reimplement a CRDT in pure Elm I guess…

Is there any known good practice ?

1 Like

I am currently developing an application in Elm which uses CRDTs - but not one of the implementations that you mention.

We use Phoenix channels to handle the communication between the web clients and server and just built our own CRDTs on top of that. The simple CRDTs like counters and sets and so on are really not hard to implement and gave us everything we need. Stuff for text editing looks much more involved, but fortunately all our text editing sits behind a mutex; only one user can edit a given piece of text at a time.

One thing I have learned is that CRDTs are not a magic solution for all multi user interactions. With the Phoenix channels we are able to rely on message ordering, which means we can get atomic events by bouncing them off the phoenix server. For example, when setting a simple property like the color of something, we send a color change message, but do not apply it until it comes back from the server, that way we will always apply a series of color changes in the correct order, and all clients will end up with a consistent UI. (take_lock and release_lock are also atomic operations, which gives you mutexes, pheonix can also detect a dropped channel and release a lock, so no issue with dangling locks).

CRDTs have been very useful for realtime user interaction though - like users editing a drawing at the same time and seeing other users move things around etc.

So depends on what you want to do. I think Elm is a great language for implementing CRDTs in, but obviously a lot of work for some of the trickier bits of multi user realtime text editing if that is what you are doing.

Also, watch this if you have not seen it yet? "ElmSEQ: A conflict-free replicated data type for elm" by Matthias Rella - YouTube

5 Likes

Seems like the basic CRDT algorithms already exist in Elm:

https://package.elm-lang.org/packages/niho/elm-crdt/latest/

2 Likes

Very interesting, thanks !

I guess I have to figure out a way to model things properly. A Dict-like type would be ideal. Maybe nested somehow…

I’m actually using YJS for a small experiment right now. It’s very plug and play. I like that you can switch out the transportation and persistence layer and it does the rest for you. It also seems to have the best performance. I have only used the awareness layer so far though.

On nested structures: I have worked with deeply nested structures and I ran into performance issues because I had to do a lot of expensive tree traversal to find the nodes that I need. I am building everything around flat dicts and references right now. You need to manually manage consistency but I must say so far I really prefer it. Obviously you are building some type of collaborative editing experience - a shallow structure will also make it easy to isolate a part of your document for a specialized view. Think of an isolated component view in figma.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.