DeviceHive proxy messages classes
DeviceHive server entities and plugins should connect to the DeviceHive WebSocket proxy DeviceHive WebSocket proxy has it's own messages formats. This module helps to work with this formats transforms raw proxy messages to JS object with fully named fields.
- Message - Base Proxy Message class
- MessageUtils - Utility Message class
- MessageBuilder - Message Builder
- PayloadNormalizer - Normalizer for Message payload
- payload:
- TopicCreatePayload - Payload for topic create request/response
- TopicListPayload - Payload for topic list request/response
- TopicSubscribePayload - Payload for topic subscribe request/response
- TopicUnsubscribePayload - Payload for topic unsubscribe request/response
- NotificationCreatePayload - Payload for notification create request
- NotificationPayload - Payload for notification push message
- PluginAuthenticatePayload - Payload for plugin authentication request
- HealthPayload - Payload for health check response
- TokenPayload - Payload for plugin token (plugin authentication response)
- ErrorPayload - Error response payload
Raw message:
{
"id": <message id>,
"t": <message type>,
"a": <message action>,
"p": <message payload>,
"s": <message status>
}
Message object:
{
"id": <message id>,
"type": <message type>,
"action": <message action>,
"payload": <message payload>,
"status": <message status>
}
Usage example:
const { Message } = require(`devicehive-proxy-message`);
const requestMessage = new Message({
"id": 1,
"type": "topic",
"action": "list"
});
const responseMessage = Message.normalize({
"id": 1,
"t": "topic",
"a": "list",
"p": { "t": ["testTopic"] },
"s": 0
});
MessageUtils is a class with next static members:
- MessageUtils.TOPIC_TYPE = "topic"
- MessageUtils.NOTIFICATION_TYPE = "notif"
- MessageUtils.HEALTH_CHECK_TYPE = "health"
- MessageUtils.PLUGIN_TYPE = "plugin"
- MessageUtils.ACK_TYPE = "ack"
- MessageUtils.NO_ACTION = undefined
- MessageUtils.CREATE_ACTION = "create"
- MessageUtils.LIST_ACTION = "list"
- MessageUtils.SUBSCRIBE_ACTION = "subscribe"
- MessageUtils.UNSUBSCRIBE_ACTION = "unsubscribe"
- MessageUtils.AUTHENTICATE_ACTION = "authenticate"
- MessageUtils.SUCCESS_STATUS = 0
- MessageUtils.FAILED_STATUS = 1
Usage example:
const { Message, MessageUtils } = require(`devicehive-proxy-message`);
const requestMessage = new Message({
"id": 1,
"type": MessageUtils.TOPIC_TYPE,
"action": MessageUtils.LIST_ACTION
});
MessageBuilder is a class with next static members:
- MessageBuilder.createTopic(payload, id), payload is an object like for TopicCreatePayload constructor, id - message id;
- MessageBuilder.listTopics(id), id - message id
- MessageBuilder.subscribeTopic(payload, id), payload is an object like for TopicSubscribePayload constructor, id - message id;
- MessageBuilder.unsubscribeTopic(payload, id) , payload is an object like for TopicUnsubscribePayload constructor, id - message id;
- MessageBuilder.createNotification(payload, id) , payload is an object like for NotificationCreatePayload constructor, id - message id;
- MessageBuilder.authenticatePlugin(payload, id) , payload is an object like for PluginAuthenticatePayload constructor, id - message id;
- MessageBuilder.health(id), id - message id
Usage example:
const { MessageBuilder } = require(`devicehive-proxy-message`);
const requestMessage = MessageBuilder.listTopics(1);
PayloadNormalizer is used by Message class in normalize method.
All fields in payloads objects prefixed by '_' and has corresponding javascript get/set function without '_' prefix
Raw payload:
{
"t": <list of topics>
}
TopicCreatePayload object:
{
"topicList": <list of topics>
}
Raw payload:
{
"t": <list of topics>
}
TopicListPayload object:
{
"topicList": <list of topics>
}
Raw payload:
{
"sg": <subscription group>,
"t": <list of topics>
}
TopicSubscribePayload object:
{
"subscriptionGroup": <subscription group>,
"topicList": <list of topics>
}
Raw payload:
{
"sg": <subscription group>,
"t": <list of topics>
}
TopicUnsubscribePayload object:
{
"subscriptionGroup": <subscription group>,
"topicList": <list of topics>,
}
Raw payload:
{
t: <topic name>,
m: <notification message>,
part: <topic partition>
}
NotificationCreatePayload object:
{
topic: <topic name>,
message: <notification message>,
partition: <topic partition>
}
Raw payload:
{
"m": <notification message>
}
NotificationPayload object:
{
"message": <notification message>
}
Raw payload:
{
"token": <plugin access token>
}
PluginAuthenticatePayload object:
{
"token": <plugin access token>
}
Raw payload:
{
prx: <proxy status>,
mb: <message buffer status>,
mbfp: <message buffer fill percentage>,
comm: <internal message broker status>
}
HealthPayload object:
{
proxy: <proxy status>,
messageBuffer: <message buffer status>,
messageBufferFillPercentage: <message buffer fill percentage>,
communicator: <internal message broker status>
}
Raw payload:
{
e: <token expiration date>,
t: <token type>,
tpc: <plugin topic>
}
TokenPayload object:
{
expirationDate: <token expiration date>,
type: <token type>,
topic: <plugin topic>
}
Raw payload:
{
m: <error message>,
c: < error code>
}
TokenPayload object:
{
message: <error message>,
code: <error code>
}