Messages

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

(function() { const messages = {"...": null}; messages[randomHex(8)] = {"#r": randomHex(16)}; messages[randomHex(8)] = {"id": "light-34", "on": true, "#": 1684254874000}; messages[randomHex(8)] = {"id": "light-67", "on": false, "#": 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 ("#": ...). The revision is usually a Unix timestamp in milliseconds since epoch. 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.

JavaScript

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

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

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

function onDone(request) {
	...
}

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

HTTP/REST Requests

Send a message

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

200 if the message was merged

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

Adds or merges a message on an inbox document. The message ID must be a 16-hexdigit token.

Delete a message

Request
DELETE /backend/documents/WRITETOKEN/messages/ID
Response

200 if the message was deleted or did not exists

403 if the document is not an inbox

Inbox write token
Message ID

Removes a message.

Message removal should be used with care. First of all, the message may already have been read by the recipient. Second, message removal is not a forward operation: after the removal, an old version of the message may get merged — either by a valid request, a concurrent request, or a (malicious) replay of a previous request.

Nevertheless message removal is useful and safe when used properly.

UDP Requests

If UDP is enabled, the following requests are available.

Send a message

Request

			
Response

			

Adds or merges a message on an inbox document, and responds with an "M" message upon success.

If the document is not an inbox, "N" is returned.

Delete a message

Request

			
Response

			

Removes a message. See above for a discussion about message removal.