MQTT
Method for making mqtt subscriptions. It`s using mqtt.js as dependency.
MQTT methods
subscribe(topic/topics array): subscribe to new topic(s). Return promise with resolved object of subscription.-
topic/topics arrayis the topic or topics to need subscribetopic = { name: 'a/b/c', options: {qos: 2}, handler: (message, topic) => { console.log(message) } }
connector.socket.subscribe({name: 'custom/info', handler: (data, topic) => { console.log(`subscribed: ${data}`) }}) .then(function (grants) { console.log(grants) }).catch(function(e) { console.log(e) })- options structure:
filterByTimestampflespi MQTT 5.0 feature that filters messages by user properties timestamp provided by flespibooleanqosqos subscription level, default 0nlNo Local MQTT 5.0 flag (If the value is true, Application Messages MUST NOT be forwarded to a connection with a ClientID equal to the ClientID of the publishing connection)rapRetain as Published MQTT 5.0 flag (If true, Application Messages forwarded using this subscription keep the RETAIN flag they were published with. If false, Application Messages forwarded using this subscription have the RETAIN flag set to 0.)rhRetain Handling MQTT 5.0 (This option specifies whether retained messages are sent when the subscription is established.)properties:objectsubscriptionIdentifier: representing the identifier of the subscriptionnumber,userProperties: The User Property is allowed to appear multiple times to represent multiple name, value pairsobject
grants structure:
{[id of subscription]: [grants by subscription]}-
unsubscribe(topicName, index/indexes, options): unsubscribe from topic (remove hanlers by it ids). Return promise.topicName: is the topic nameindex: index or array of indexes of current subscription by topicoptions: options of unsubscribe.properties:objectuserProperties: The User Property is allowed to appear multiple times to represent multiple name, value pairsobjectconnector.socket.unsubscribe('custom/logs') connector.socket.unsubscribe('custom/logs', 1) connector.socket.unsubscribe('custom/logs', [1, 2, 3]) connector.socket.unsubscribe('custom/logs', undefined, {properties})
unsubscribeAll([options]): unsubscribe from all topicsoptions: options of unsubscribe.properties:objectuserProperties: The User Property is allowed to appear multiple times to represent multiple name, value pairsobject
connector.socket.unsubscribeAll()publish(topic, message, [options]): publish a message to a topic. Return promise.topicis the topic to publish to,Stringmessageis the message to publish,BufferorStringoptionsare the options to publish with, including:qosQoS level,Number, default0retainretain flag,Boolean, defaultfalsedupmark as duplicate flag,Boolean, defaultfalseproperties: MQTT 5.0 propertiesobjectpayloadFormatIndicator: Payload is UTF-8 Encoded Character Data or notboolean,messageExpiryInterval: the lifetime of the Application Message in secondsnumber,topicAlias: value that is used to identify the Topic instead of using the Topic Namenumber,responseTopic: String which is used as the Topic Name for a response messagestring,correlationData: used by the sender of the Request Message to identify which request the Response Message is for when it is receivedbinary,userProperties: The User Property is allowed to appear multiple times to represent multiple name, value pairsobject,subscriptionIdentifier: representing the identifier of the subscriptionnumber,contentType: String describing the content of the Application Messagestring
connector.socket.publish('custom/info', JSON.stringify({name: 'device#269'}))close([force], [options]): close connectionforceis flag force closing of connectionoptions: options of disconnect.reasonCode: Disconnect Reason Codenumberproperties:objectsessionExpiryInterval: representing the Session Expiry Interval in secondsnumber,reasonString: representing the reason for the disconnectstring,userProperties: The User Property is allowed to appear multiple times to represent multiple name, value pairsobject,serverReference: String which can be used by the Client to identify another Server to usestringconnector.socket.close()
connected(): return status of connectionconnector.socket.connected()on(event, handler): subscribe connector to event. It returns index of handler. It needed for off current handler.eventis the event type to subscribehandleris the handler for current event type to subscribe
Vue.connector.socket.on('connect', () => { console.log('Working!') })off(event, [index]): unsubscribe connector from eventeventis the event type to unsubscribeindexis the handler’s index returned methodofffor current event type need to unsubscribe
Vue.connector.socket.off('connect', 1)Methods to easily subscribe and unsubscribe via Flespi MQTT Broker. Param handler is a function to process new messages from mqtt. Example:
(messages) => { console.log(messages) }Other params described at Flespi MQTT Topics
Example of use
connector.socket.logs.subscribe(api, origin, event_type, handler)
// or
connector.subscribeLogs(api, origin, event_type, handler)
Return promise with resolved index of subscription.
| Method | Alias | Params | Description |
|---|---|---|---|
| socket.logs.subscribe | subscribeLogs | api, origin, event_type, handler, [options] | Subscribe to logs. {event_type} has occurred in {origin}, see platform logs for more information |
| socket.logs.unsubscribe | unsubscribeLogs | api, origin, event_type, [indexOfSubscription], [options] | Unsubscribe from logs. {event_type} has occurred in {origin}, see platform logs for more information |
| socket.messages.channels.subscribe | subscribeMessagesChannels | channel_id, ident, handler, [options] | Subscribe to new message received by channel |
| socket.messages.channels.unsubscribe | unsubscribeMessagesChannels | channel_id, ident, [indexOfSubscription], [options] | Unsubscribe from new message received by channel |
| socket.messages.devices.subscribe | subscribeMessagesDevices | device_id, handler, [options] | Subscribe to new message received by device |
| socket.messages.devices.unsubscribe | unsubscribeMessagesDevices | device_id, [indexOfSubscription], [options] | Unsubscribe from new message received by device |
| socket.messages.sms.subscribe | subscribeSms | modem_id, phone, handler, [options] | Subscribe to SMS message received by modem |
| socket.messages.sms.unsubscribe | unsubscribeSms | modem_id, phone, [indexOfSubscription], [options] | Unsubscribe from SMS message received by modem |
| socket.state.subscribe | subscribeState | api, origin, id, handler, [options] | Subscribe to entities object with default properties |
| socket.state.unsubscribe | unsubscribeState | api, origin, id, [indexOfSubscription], [options] | Unsubscribe from entities object with default properties |
| socket.state.properties.subscribe | subscribeStateProperties | api, origin, id, property, handler, [options] | Subscribe to entities properties |
| socket.state.properties.unsubscribe | unsubscribeStateProperties | api, origin, id, property, [indexOfSubscription], [options] | Unsubscribe from entities properties |
| socket.state.devices.telemetry.subscribe | subscribeStateDevicesTelemetry | id, parameter, handler, [options] | Subscribe to telemetry of devices |
| socket.state.devices.telemetry.unsubscribe | unsubscribeStateDevicesTelemetry | id, parameter, [indexOfSubscription], [options] | Unsubscribe from telemetry of devices |
| socket.state.devices.settings.subscribe | subscribeStateDevicesSettings | id, name, handler, [options] | Subscribe to telemetry of devices |
| socket.state.devices.settings.unsubscribe | unsubscribeStateDevicesSettings | id, name, [indexOfSubscription], [options] | Unsubscribe from telemetry of devices |
| socket.intervals.subscribe | subscribeIntervals | calc_id, device_id, event, handler, [options] | Subscribe to intervals events |
| socket.intervals.unsubscribe | unsubscribeIntervals | calc_id, device_id, event, [indexOfSubscription], [options] | Unsubscribe from intervals events |
- subscribe like handlers options structure:
prefixtopic prefix like$filter/{filter}qosqos subscription level, default 0nlNo Local MQTT 5.0 flag (If the value is true, Application Messages MUST NOT be forwarded to a connection with a ClientID equal to the ClientID of the publishing connection)rapRetain as Published MQTT 5.0 flag (If true, Application Messages forwarded using this subscription keep the RETAIN flag they were published with. If false, Application Messages forwarded using this subscription have the RETAIN flag set to 0.)rhRetain Handling MQTT 5.0 (This option specifies whether retained messages are sent when the subscription is established.)properties:objectsubscriptionIdentifier: representing the identifier of the subscriptionnumber,userProperties: The User Property is allowed to appear multiple times to represent multiple name, value pairsobject
- unsubscribe like handlers options structure:
prefixtopic prefix like$filter/{filter}properties:objectuserProperties: The User Property is allowed to appear multiple times to represent multiple name, value pairsobject
MQTT events
Event connect: Emitted on successful (re)connection. In handler provided connack packet.
function handler(connack) {}
Event error: Emitted when the client cannot connect
function handler(error) {}
// error_example = {message: 'connection refused, identifier rejected', code: 2}
* `code: 2`: connection refused, identifier rejected
* `code: 3`: connection refused, server unavailable
* `code: 5`: connection refused, not authorized
Event close: Emitted after disconnect
function () {}
Event disconnect: Emitted after disconnect by broker
function (closePacket) {}
Event reconnect: Emitted after reconnect start
function () {}
Event offline: Emitted when the client goes offline
function () {}
Event end: Emitted when the client goes end
function () {}