Giter Site home page Giter Site logo

javascript's Introduction

NPM NPM

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.

Installation

Emitter for NodeJS:

npm install emitter-io --save

Emitter for the Browser:

Example

// 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!"
});

API


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.


Emitter()

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

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 'keygen'

function(keyJson) {}

Emitted when the client generate a key to a channel using Emitter#keygen() function.

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#disconnect()

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).

  • key is security key to use for the operation, String
  • channel is the channel string to publish to, String
  • name is the 2-character name of the link, String
  • private requests the creation of a private channel, Boolean
  • message is the message to publish, Buffer or String
  • ttl is the time to live of the messages that will be sent through the link, Number.
  • me tells whether the messages sent through the link should be also sent to the publisher, Boolean. By default it is set to true.

See also publishWithLink().


Emitter#publish({ key: string; channel: string; message: any; ttl?: number; me?: boolean; })

Publishes a message to a channel

  • key is security key to use for the operation, String
  • channel is the channel string to publish to, String
  • message is the message to publish, Buffer or String
  • ttl is the time to live of the message, Number
  • me tells whether the messages should be also sent to the publisher, Boolean. By default it is set to true.

Emitter#publishWithLink({ link: string; message: any; })

Publishes a message to a link.

  • link is the name of the link, String
  • message is the message to publish, Buffer or String

See also link().


Emitter#subscribe({ key: string; channel: string; })

Subscribes to a channel

  • key is security key to use for the operation, String
  • channel is the channel string to subscribe to, String

Emitter#unsubscribe({ key: string; channel: string; })

Unsubscribes from a channel

  • key is security key to use for the operation, String
  • channel is the channel string to unsubscribe from, String

Emitter#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, String
  • channel is the channel string to generate a key for, String
  • type the type of the key to generate. Possible options include r for read-only, w for write-only, p for presence only and rw for read-write keys (In addition to rw, you can use any combination of r, w and p for key generation), String
  • ttl is the time-to-live of the key, in seconds.

Emitter#me()

Retrieves information about the underlying client connection. Information includes the client ID and the links created by the client.


Emitter#presence({ key: string; channel: string; status: boolean; changes: boolean; })

Requests the presence for a particular channel.

  • key is master/secret key to use for the operation, String
  • channel is the channel string to generate a key for, String
  • status whether the current state should be retrieved or not
  • changes whether the future changes should be received or not

EmitterMessage()

The EmitterMessage class wraps a message received from the broker. It contains several properties:

  • channel is channel the message was published to, String
  • binary is the buffer associated with the payload, Buffer

EmitterMessage#asString()

Returns the payload as a utf-8 String.


EmitterMessage#asBinary()

Returns the payload as the Buffer.


EmitterMessage#asObject()

Returns the payload as JSON-deserialized Object.

License

The MIT License (MIT) Copyright (c) 2016 Misakai Ltd.

javascript's People

Contributors

anjucheran avatar dawsontoth avatar florimond avatar joaopaulobdac avatar kelindar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

javascript's Issues

Async functions for keygen, presence and me

Is it possible to implement async versions of keygen, presence (status = true, changes = false) and me functions?

For example:

var response = await keygenAsync({ key: key_, channel: channel_, ttl: 60 });
console.log(response.key);

I can implement it myself but since there is global event handler for all three requests, I have to find a way to match the request I send with the response I get.

Do emitter requests have an id which I can use for this purpose?

How to modify and rebuild?

Hi,
I want to add some async functions to the library.

What is the proper way of modifying this library and rebuild it?

Should I modify emitter.ts in the lib directory?

Using in React

I've been struggling trying to get this to work in React. Does anyone have any guidance for making that work? I've tried manually importing the emitter.min.js in the index.html but I just get more errors I think because it is missing some dependencies.

Unable to connect using the free account

Hi,
I use emitter.io in an Angular and in a dotnetcore application. Both application now throw an exception if I try to connect => the last successfull connect was 2019-05-10. I updated the Angular version to 1.38.0 but the problem is still same. I didn´t modified the sources in the last 2 weeks.

The Angular application returns
stream.js:43 WebSocket connection to 'ws://api.emitter.io:8080/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

and the dotnetcore throws
ExtendedSocketException: Es konnte keine Verbindung hergestellt werden, da der Zielcomputer die Verbindung verweigerte 35.193.233.222:8080 (No connection could be established because the target computer refused the connection.)
2019-05-16 17:22:24,607 [10] FATAL TheHaulierApi.Services.Emitter_IO.EmitterIOService - EmitterIOService(..), Exception: Exception connecting to the broker

It is not a firewall problem, the connect failed in local and the deployed (Azure) versions.

Any idea, hint?
Regards
Juergen

Does not auto-reconnect

If i restart the broker, the client does not auto-reconnect.

But npm doc, connect() says it automatically reconnects.

HTTPS CDN

Is there an HTTPS CDN for the Javascript library?

Recent updates are not released

Although there were updates in the repository

#16

I still see that the released packages uses old type definitions

    /**
     * Hooks an event to the client.
     */
    on(event: EmitterEvents | string, callback: (args?: any) => void): Emitter;
    /**
     * Unhooks an event from the client.
     */
    off(event: EmitterEvents | string, callback: (args?: any) => void): Emitter;

Can you release the new version of the package?

keygen Example

@kelindar can u gave me an example creating key using keygen for specific channel and how can i retrieve the value.....

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.