After a long time, I’ve decided to go back to realtime collaboration on Phoenix. The plan is to implement something compatible with
ShareDB's protocol. There are already some tools using
ShareDB in the wild, and the most important case (collaborative editing of text) is already covered by ´ShareDB` pretty well (there’s a demo under 20 LOC doing exactly that with some extensions).
The client-side API for ShareDB starts with something like this:
var ShareDB = require('sharedb/lib/client'); var socket = new WebSocket('ws://localhost:8080'); var connection = new ShareDB.Connection(socket);
socket really must be a
WebSocket, or at least something that implements the websocket methods. That’s actually very easy with Phoenix channels: you just have to create an object (class, prototype, whatever) which implements those methods and behaves as a webocket. However, there is a problem.
ShareDB allows you to create several “documents”, which can be updated in parallel. That means each document should live in its own process and be connected to the client though its own topic. Our “custom” websocket implementation would have to route each message into the appropriate phoenix topic. But the
WebSocket object is sent raw JSON and doesn’t know which document/topic the message refers to. The only way to know that is to parse the JSON again and extract the document’s ID, which is unacceptable.
This means I will have to rewrite both the
Connection object and the
WebSocket object so that the
WebSocket is compatible with Phoenix channels and the
Connection object can send objects into the
WebSocket (so that it can route the messages into the correct topics).