Elko: JSON Messaging over HTTP

JSON Messaging Over HTTP

JSON Messaging is a set of conventions for using JSON to encode a bidirectional object-to-object message connection between two machines. The underlying transport abstraction assumed is that of TCP: a simple, persistent, bidirectional communications pipe. However, various practicalities, notably the desire to enable unmodified web browsers to act as clients, dictate that we be able to use HTTP as the underlying transport layer.

Since HTTP is stateless and sessionless, an additional transport protocol is required when the message transport medium is HTTP. This protocol uses special URLs and additional JSON encoding within the HTTP request and reply bodies to efficiently synthesize a sessionful, symmetric, bidirectional, asynchronous message channel out of an ongoing series of transient, asymmetric, synchronous, RPC-style HTTP requests. This document describes the details of this transport protocol.

Client Connection

GET ROOT/connect
   or
GET ROOT/connect/RANDOMSTUFF

This initiates a new application-level session.

The HTTP reply body contains the JSON:

{
sessionid: SESSIONID
}

where:

Server to Client Messaging

GET ROOT/select/SESSIONID/SSEQNUM

This is a poll for messages from the server to the client. (Actually, it is more like Unix's select() combined with read()).

The HTTP reply body contains the JSON:

{
msgs: [MSG, MSG, ...],
seqnum: NEWSSEQNUM
}

where:

Client to Server Messaging

POST ROOT/xmit/SESSIONID/XSEQNUM

This is a delivery of one or more messages from the client to the server.

The messages are transmitted, in the form of sequential JSON message literals, as the HTTP request body.

The HTTP reply body contains the JSON:

{
seqnum: NEWXSEQNUM,
}

where:

Client Disconnection

GET ROOT/disconnect/SESSIONID

This terminates an application level session.

The HTTP reply body contains the JSON:

{
}

Errors

Sometimes the browser may become confused (due to browser bugs, for example) about the appropriate URL to use for sending or receiving messages. In such a case, it may send an inappropriate HTTP request to the server that will contain an invalid sequence number or session ID.

In the case of such an error, the server will respond with the reply:

{
error: ERRORNAME
}

where: