Messages

Documents with a $messages key at the first level are inbox documents, and accept messages:

(function() { const messages = {"...": null}; messages[randomHex(8)] = {"read token": randomHex(16)}; messages[randomHex(8)] = {"id": "light-34", "on": true, "revision": 1684254874000}; messages[randomHex(8)] = {"id": "light-67", "on": false, "revision": 1684254903000}; return {"$messages": messages, "...": null}; })()

Messages are sent using the write token of the document. For that purpose, the write token of an inbox document is shared with all potential senders. The read token is kept secret by the owner.

Messages are small JSON documents of up to 4096 bytes. They may contain some data, or refer to other documents or blobs.

Every message has an 16-hexdigit message ID, which is randomly generated by the sender. Messages can be updated by sending a message with the same ID, and a newer revision field (a number). If no revision field is present, the revision is assumed to be 0.

The owner can use an inbox document like any other document. To read the messages, it loads the document, and typically removes all processed messages from the list.

HTTP/REST Requests

POST /backend/documents/WRITETOKEN/messages/ID
JSON MESSAGE

Adds or updates a message on an inbox document. ID is a 16-hexdigit token, randomly generated by the sender.

If a message with the same ID exists, the message with the larger revision number is kept.

Response

  • 200
  • 400, if the message is not a valid JSON document
  • 403, if the document is not an inbox
  • 413, if the message is too large
Inbox write token
Message ID
Message
DELETE /backend/documents/WRITETOKEN/messages/ID

Removes a message. Note that the message may already have been read by the recipient.

Response

  • 200
  • 403, if the document is not an inbox
Inbox write token
Message ID

JavaScript

Using GenericBackend.js, a message can be sent as follows:

const backend = new GenericBackend('https://viereck.ch/backend');

const writeToken = '1b599fd0...';
const messageId = backend.randomMessageid();
const message = {
	revision: new Date().getTime(),
	type: 'temperature',
	value: 21.1
	};
backend.sendMessage(writeToken, messageId, message, onDone, onError);

function onDone(request) {
	...
}

function onError(errorCode, request) {
	...
}