Git Product home page Git Product logo

paho.mqtt.js's Introduction

Paho.mqtt.js

js-standard-style

The Paho JavaScript Client is a browser-based library that uses WebSockets to connect to an MQTT server. fork from eclipse.paho.mqtt

Only expose a single object name in the global namespace. Everything must go through this module. Global Paho.MQTT module only has a single public function, client, which returns a Paho.MQTT client object given connection details.

The source of the client is in:

    src/mqttws31.js

Send & Receive messages using web browsers.

This programming interface lets a Javascript client application use the MQTT V3.1 or V3.1.1 protocol to connect to an MQTT-supporting messaging server.

Then function supported includes:

  • Connecting to and disconnecting from server.The server is identified by its host name and port number.
  • Specifying options that relate to the communications link with the server, for example the frequency of keep-alive heartbeats, and whether SSL/TLS is required.
  • Subscribing to and receiving messages from MQTT Topics.
  • Publishing messages to MQTT Topics.

The API consists of two main objects:

{@link Paho.MQTT.Client}

This contains methods that provide the functionality of the API, including provision of callbacks that notify the application when a message arrives from or is delivered to the messaging server, or when the status of its connection to the messaging server changes.

{@link Paho.MQTT.Message}

This encapsulates the payload of the message along with various attributes associated with its delivery, in particular the destination to which it has been (or is about to be) sent.

The programming interface validates parameters passed to it, and will throw an Error containing an error message intended for developer use, if it detects an error with any parameter.

Example for browser globals

<script src="./mqttws31js"></script>
client = new Paho.MQTT.Client(location.hostname, Number(location.port), "clientId");
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect});
function onConnect() {
  // Once a connection has been made, make a subscription and send a message.
  console.log("onConnect");
  client.subscribe("/World");
  message = new Paho.MQTT.Message("Hello");
  message.destinationName = "/World";
  client.send(message);
};
function onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0)
    console.log("onConnectionLost:"+responseObject.errorMessage);
};
function onMessageArrived(message) {
  // If the transmitted data is non-UTF8 formatted data, do not use payloadString
  // It will throw an error from parseUTF8. use payloadBytes instead.
  console.log("onMessageArrived:"+message.payloadString);
  client.disconnect();
};

Example for AMD

define(['paho.mqtt.js'], function (PahoMQTT) {
    client = new PahoMQTT.Client(location.hostname, Number(location.port), "clientId");
})

Example for Node, CommonJS-like

var PahoMQTT = require('paho.mqtt.js')
client = new PahoMQTT.Client(location.hostname, Number(location.port), "clientId");

paho.mqtt.js's People

Contributors

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