Giter Site home page Giter Site logo

mqtt.js's Introduction

mqtt.js Build Status

Introduction

NPM NPM

mqtt.js is a library for the MQTT protocol, written in JavaScript to be used in node.js.

Important notes for existing users

  • v0.3.0 improves connection stability, performance, the reconnection logic and SSL support. See #118 for details. A Connection is a Writable stream, so you can run MQTT.js over any kind of Stream (doc needed). Both the constructors of MqttClient and MqttConnection changed, but not the factory method mqtt.createClient and mqtt.createConnection.

  • v0.2.0 has brough some API breaking changes to mqtt.js. Please consult the migration guide for information or open an issue if you need any help.

Installation

npm install mqtt

Example

First you will need to install and run a broker, such as Mosquitto or Mosca, and launch it.

For the sake of simplicity, let's put the subscriber and the publisher in the same file:

var mqtt = require('mqtt')

client = mqtt.createClient(1883, 'localhost');

client.subscribe('presence');
client.publish('presence', 'Hello mqtt');

client.on('message', function (topic, message) {
  console.log(message);
});

client.end();

output:

Hello mqtt

If you do not want to install a separate broker, you can try using the server/orig example. It implements enough of the semantics of the MQTT protocol to run the example.

Documentation

Detailed documentation can be found in the wiki

Client API usage

See: examples/client

Simple publish client

var mqtt = require('mqtt')
  , client = mqtt.createClient();

client.publish('messages', 'mqtt');
client.publish('messages', 'is pretty cool');
client.publish('messages', 'remember that!', {retain: true});
client.end();

Simple subscribe client

var mqtt = require('mqtt')
  , client = mqtt.createClient();

client.subscribe('messages');
client.publish('messages', 'hello me!');
client.on('message', function(topic, message) {
  console.log(message);
});
client.options.reconnectPeriod = 0;  // disable automatic reconnect

Connect using a URL

Using the connect method, which can create either a normal or secure MQTT client.

var mqtt = require('mqtt')
  , client = mqtt.connect('mqtt://user:pass@localhost?clientId=123abc');

client.subscribe('messages');
client.publish('messages', 'hello me!');
client.on('message', function(topic, message) {
  console.log(message);
});

Supports mqtt:// and tcp:// for normal connections, and mqtts:// or ssl:// for secure connections.

As seen above the clientId can be passed in as a query parameter.

Chainable API!

var mqtt = require('mqtt')
  , client = mqtt.createClient();

client
  .subscribe('messages')
  .publish('presence', 'bin hier')
  .on('message', function(topic, message) {
    console.log(topic);
  });

Server API usage

Broadcast server example

Included in examples/broadcast.js:

var mqtt = require('mqtt');

mqtt.createServer(function(client) {
  var self = this;

  if (!self.clients) self.clients = {};

  client.on('connect', function(packet) {
    client.connack({returnCode: 0});
    client.id = packet.clientId;
    self.clients[client.id] = client;
  });

  client.on('publish', function(packet) {
    for (var k in self.clients) {
      self.clients[k].publish({topic: packet.topic, payload: packet.payload});
    }
  });

  client.on('subscribe', function(packet) {
    var granted = [];
    for (var i = 0; i < packet.subscriptions.length; i++) {
      granted.push(packet.subscriptions[i].qos);
    }

    client.suback({granted: granted, messageId: packet.messageId});
  });

  client.on('pingreq', function(packet) {
    client.pingresp();
  });

  client.on('disconnect', function(packet) {
    client.stream.end();
  });

  client.on('close', function(err) {
    delete self.clients[client.id];
  });

  client.on('error', function(err) {
    client.stream.end();
    console.log('error!');
  });
}).listen(1883);

License

MIT

mqtt.js's People

Contributors

mcollina avatar radekg avatar kmpm avatar samirnaik avatar anjz avatar wolfeidau avatar oribrost avatar davedoesdev avatar seubert avatar keithamus avatar oren avatar timoxley avatar thatguydan avatar jlbribeiro avatar mrose17 avatar paulbellamy avatar paultanner avatar ralight avatar jz-hc avatar freehaha avatar ipconfiger avatar paularmand avatar

Watchers

James Cloos avatar

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.