Skip to content

Protocol

jbyuki edited this page Mar 6, 2021 · 10 revisions

This is a prototype. All protocol decisions are subject to change rapidely!

Protocol

The protocol is as follows:

  • The first client connects to the server via InstantStartSingle

    • Websocket handshake
    • Send an info message from client
    • The server responds with available message
      • The client check if is_first is true
      • The client check if session_share mode matches
  • A second client connects to the server via InstantJoinSingle

    • Websocket handshake
    • Send an info message from client
      • The client check if is_first is false
      • The client check if session_share mode matches
    • The client sends a request message
    • Server sends the request to another already connected client
    • Client responds with initial message which contains the whole content
      • If several buffer are shared, initial messages are sent individually
    • Server broadcasts to clients
    • Client receives initial message and sets the buffer content
      • Note: other clients also receives it but discards it
  • During text edit:

    • Client send text message with edits
    • Server broadcast to other clients
    • Client receive the edit and apply it to its content
      • Note: No text is stored on the server. The server only acts as a broadcast

Messages

All messages are encoded in JSON.

The info message by the client when it first connects.

[
        MSG_INFO, // message type [integer]
        session_share, // client request session share? [boolean]
        username, // client name [boolean]
]

The available message sent by the server in response to the client.

[
        MSG_AVAILABLE, // message type [integer]
        is_first, // first client to connect to the server? [boolean]
        client_id, // unique client id assigned by the server [integer]
        session_share, // server in session share mode? [boolean]
        
]

The request message. This is sent when a client joins a server. It asks for current data. The server relays this message to an already connected client.

[
        MSG_REQUEST, // message type [integer]
]

The initial message sent by a client to set the initial data in buffers.

[
	MSG_INITIAL, // message type [integer]
        buffer_name, // [string]
        [ 
             bufnr, // buffer number in creator client [integer]
             client_id // client id [integer]
        ],  // buffer unique identifier 
        
        pids, // list of pids of the initial content [integer[]]
        content, // list of lines with the initial content [string[]]
]

The text message. It describes individual character operation.

[
	MSG_TEXT, // message type [integer]
        op, // text character operation [operation]
        [ 
             bufnr, // buffer number in creator client [integer]
             client_id // client id [integer]
        ],  // buffer unique identifier 
        client_id, // client id of sender [integer]
]

An operation can be an character insert operation.

[
	OP_INS, // operation type [integer]
        c, // character to insert [string]
	new_pid, // pid of inserted character [pid]
]

An operation can be an character delete operation.

[
	OP_DEL, // operation type [integer]
        c, // character to delete [string]
        del_pid, // pid of character to delete [pid]
]

Note: The character to delete must not be explicitly told normally but it's convenient for the undo/redo implementation.

The connect message sent when a client connects.

[
	MSG_CONNECT, // message type [integer]
	client_id, // id of newly connected client [integer]
	username, // username of newly connected client [string] 
]

The disconnect message sent when a client disconnects.

[
	MSG_DISCONNECT, // message type [integer]
	client_id, // id of the disconnected client [integer]  
]

The data message sent by a client. It can contain any data.

[
	MSG_DATA, // message type [integer]
	data,
]

The mark message sent by a client to visually mark a text region.

[
	MSG_MARK, // message type [integer]
	client_id, // client id of sender [integer]
	[ 
             bufnr, // buffer number in creator client [integer]
             client_id // client id [integer]
	],  // buffer unique identifier 
	spid, // pid of first character [pid]
	epid, // pid of last character [pid]
]
Clone this wiki locally