This repository contains JavaScript client for both NodeJS and the Browser for Emitter (see also on Emitter GitHub). Emitter is an open-source real-time communication service for connecting online devices. At its core, emitter.io is a distributed, scalable and fault-tolerant publish-subscribe messaging platform based on MQTT protocol and featuring message storage.
Emitter for NodeJS:
npm install emitter-io --save
Emitter for the Browser:
// connect to emitter.io and get the client
var client = emitter.connect(); // or: require('emitter-io') 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#disconnect()Emitter#publish()Emitter#subscribe()Emitter#unsubscribe()Emitter#keygen()Emitter#link()Emitter#me()Emitter#presence()EmitterMessage()EmitterMessage#asString()EmitterMessage#asBinary()EmitterMessage#asObject()
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:
- Regular server pings
- QoS flow
- Automatic reconnections
- Start publishing before being connected
function(connack) {}
Emitted on successful (re)connection (i.e. connack rc=0).
connackreceived connack packet. Whencleanconnection option isfalseand server has a previous session forclientIdconnection option, thenconnack.sessionPresentflag istrue. When that is the case, you may rely on stored session and prefer not to send subscribe commands for the client.
function() {}
Emitted after a disconnection.
function() {}
Emitted when the client goes offline.
function(error) {}
Emitted when the client cannot connect (i.e. connack rc != 0) or when a parsing error occurs.
function(keyJson) {}
Emitted when the client generate a key to a channel using Emitter#keygen() function.
function(message) {}
Emitted when the client receives a message packet. The message object will be of EmitterMessage class, encapsulating the channel and the payload.
Disconnects from the remote broker
Emitter#link({ key: string; channel: string; name: string; private: boolean; message: any; ttl?: number; me?: boolean; })
Creates a 2-character link to a channel. The channel may be private. For more information about this feature, see Emitter: Simplify Client/Server and IoT Apps with Links and Private Links (on YouTube) and the Emitter Pull Request (on GitHub).
keyis security key to use for the operation,Stringchannelis the channel string to publish to,Stringnameis the 2-character name of the link,Stringprivaterequests the creation of a private channel,Booleanmessageis the message to publish,BufferorStringttlis the time to live of the messages that will be sent through the link,Number.metells whether the messages sent through the link should be also sent to the publisher,Boolean. By default it is set totrue.
See also publishWithLink().
Publishes a message to a channel
keyis security key to use for the operation,Stringchannelis the channel string to publish to,Stringmessageis the message to publish,BufferorStringttlis the time to live of the message,Numbermetells whether the messages should be also sent to the publisher,Boolean. By default it is set totrue.
Publishes a message to a link.
linkis the name of the link,Stringmessageis the message to publish,BufferorString
See also link().
Subscribes to a channel
keyis security key to use for the operation,Stringchannelis the channel string to subscribe to,String
Unsubscribes from a channel
keyis security key to use for the operation,Stringchannelis the channel string to unsubscribe from,String
Sends a key generation request to the server.
keyis master/secret key to use for the operation,Stringchannelis the channel string to generate a key for,Stringtypethe type of the key to generate. Possible options includerfor read-only,wfor write-only,pfor presence only andrwfor read-write keys (In addition torw, you can use any combination ofr,wandpfor key generation),Stringttlis the time-to-live of the key, in seconds.
Retrieves information about the underlying client connection. Information includes the client ID and the links created by the client.
Requests the presence for a particular channel.
keyis master/secret key to use for the operation,Stringchannelis the channel string to generate a key for,Stringstatuswhether the current state should be retrieved or notchangeswhether the future changes should be received or not
The EmitterMessage class wraps a message received from the broker. It contains several properties:
channelis channel the message was published to,Stringbinaryis the buffer associated with the payload,Buffer
Returns the payload as a utf-8 String.
Returns the payload as the Buffer.
Returns the payload as JSON-deserialized Object.
The MIT License (MIT) Copyright (c) 2016 Misakai Ltd.

