This is the JavaScript library for the browser and NodeJS, built and maintained by emitter.io team. The underlying communication layer uses MQTT.js and released under MIT license.
Emitter for NodeJS:
npm install emitter-io --saveEmitter for the Browser:
var client = emitter.connect();
// use require('emitter-io').connect() on NodeJS
// once we're connected, subscribe to the 'chat' channel
client.subscribe({
key: "<channel key>",
channel: "chat"
});
// on every message, print it out
client.on('message', function(msg){
console.log( msg.asString() );
});
// publish a message to the chat channel
client.publish({
key: "<channel key>",
channel: "chat/my_name",
message: "hello, emitter!"
});connect()Emitter()Emitter#publish()Emitter#subscribe()Emitter#unsubscribe()Emitter#disconnect()EmitterMessage()EmitterMessage#asString()EmitterMessage#asBinary()EmitterMessage#asObject()connect(host: string, port: number)
Connects to the emitter api broker specified by the given url and options and returns an Emitter instance. The URL can be on the following protocols: 'mqtt', 'mqtts', 'tcp', 'tls', 'ws', 'wss'. The URL can also be an object as returned by URL.parse(), in that case the two objects are merged, i.e. you can pass a single object with both the URL and the connect options.
The Emitter class wraps a client connection to an emitter.io MQTT broker over an arbitrary transport method (TCP, TLS, WebSocket, ecc). It automatically handles the following by with help of MQTT.js client:
Event 'connect'
function(connack) {}
Emitted on successful (re)connection (i.e. connack rc=0).
connack received connack packet. When clean connection option is false and server has a previous session
for clientId connection option, then connack.sessionPresent flag is true. When that is the case,
you may rely on stored session and prefer not to send subscribe commands for the client. Event 'disconnect'
function() {}
Emitted after a disconnection.
Event 'offline'
function() {}
Emitted when the client goes offline.
Event 'error'
function(error) {}
Emitted when the client cannot connect (i.e. connack rc != 0) or when a parsing error occurs.
Event 'message'
function(message) {}
Emitted when the client receives a message packet. The message object will be of EmitterMessage class, encapsulating the channel and the payload.
Emitter#publish({ key: string; channel: string; message: any; })
Publish a message to a channel
key is security key to use for the operation, Stringchannel is the channel string to publish to, Stringmessage is the message to publish, Buffer or StringEmitter#subscribe({ key: string; channel: string; })
Subscribes to a channel
key is security key to use for the operation, Stringchannel is the channel string to subscribe to, StringEmitter#unsubscribe({ key: string; channel: string; })
Unsubscribes from a channel
key is security key to use for the operation, Stringchannel is the channel string to unsubscribe from, StringEmitter#keygen({ key: string; channel: string; type: string; ttl: number; })
Sends a key generation request to the server.
key is master/secret key to use for the operation, Stringchannel is the channel string to generate a key for, Stringtype the type of the key to generate. Possible options include r for read-only, w for write-only and rw for read-write keys, Stringttl is the time-to-live of the key, in seconds.Disconnects from the remote broker
The EmitterMessage class wraps a message received from the broker. It contains several properties:
channel is channel the message was published to, Stringbinary is the buffer associated with the payload, BufferReturns the payload as a utf-8 String.
Returns the payload as the Buffer.
Returns the payload as JSON-deserialized Object.