Skip to content

devicehive/devicehive-proxy-message

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

devicehive-proxy-message

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.

Module structure

  • 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

Message

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

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

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.

Payloads

All fields in payloads objects prefixed by '_' and has corresponding javascript get/set function without '_' prefix

TopicCreatePayload

Raw payload:

{
    "t": <list of topics>
}

TopicCreatePayload object:

{
    "topicList": <list of topics>
}

TopicListPayload

Raw payload:

{
    "t": <list of topics>
}

TopicListPayload object:

{
    "topicList": <list of topics>
}

TopicSubscribePayload

Raw payload:

{
    "sg": <subscription group>,
    "t": <list of topics>
}

TopicSubscribePayload object:

{
    "subscriptionGroup": <subscription group>,
    "topicList": <list of topics>
}

TopicUnsubscribePayload

Raw payload:

{
    "sg": <subscription group>,
    "t": <list of topics>
}

TopicUnsubscribePayload object:

{
    "subscriptionGroup": <subscription group>,
    "topicList": <list of topics>,
}

NotificationCreatePayload

Raw payload:

{
    t: <topic name>,
    m: <notification message>,
    part: <topic partition>
}

NotificationCreatePayload object:

{
    topic: <topic name>,
    message: <notification message>,
    partition: <topic partition>
}

NotificationPayload

Raw payload:

{
    "m": <notification message>
}

NotificationPayload object:

{
    "message": <notification message>
}

PluginAuthenticatePayload

Raw payload:

{
    "token": <plugin access token>
}

PluginAuthenticatePayload object:

{
    "token": <plugin access token>
}

HealthPayload

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>
}

TokenPayload

Raw payload:

{
    e: <token expiration date>,
    t: <token type>,
    tpc: <plugin topic>
}

TokenPayload object:

{
    expirationDate: <token expiration date>,
    type: <token type>,
    topic: <plugin topic>
}

ErrorPayload

Raw payload:

{
    m: <error message>,
    c: < error code>
}

TokenPayload object:

{
    message: <error message>,
    code: <error code>
}

About

DeviceHive proxy messages classes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published