Giter VIP home page Giter VIP logo

mqtt.js-client-example's Introduction

MQTT.js Client Example

Overview

This is an MQTT client example project that showcases how you can use HiveMQ Cloud with the MQTT.js Client. The example project covers the basic MQTT functionality: Connecting MQTT clients to your HiveMQ Cloud cluster, subscribing to topics and publishing data (sending and receiving messages using the MQTT protocol).

The Javascript client used here supports MQTT v3.1, v3.1.1 and v5. You can find documentation for this client library here: https://www.hivemq.com/blog/mqtt-client-library-mqtt-js/.

This example repository is easily and clearly structured, so you can start quickly: This readme file is your starting point. Here we will describe what you have to do step-by-step to get started with this example. example.js is a simple implementation that demonstrates the core functionality of an MQTT Client. Follow the instructions in the following paragraphs to get started yourself.

HiveMQ Cloud

HiveMQ is the industry leader for enterprise-ready, beautifully scalable, large-scale IoT deployments with MQTT. We help companies connect things to the Internet. Our MQTT-based messaging platform ensures fast, reliable, and secure movement of data to and from connected IoT devices for companies around the world. HiveMQ Cloud is our fully-managed, cloud-native IoT messaging platform that makes trustworthy and scalable IoT device connectivity simple. You can learn more about HiveMQ Cloud on our website, documentation and our blog posts.

Getting started

By signing up for HiveMQ Cloud you will automatically get access to a HiveMQ Cloud Basic cluster. HiveMQ Cloud Basic is our smallest package that allows you to connect up to 100 MQTT clients for free and test the full MQTT functionality.

The HiveMQ Cloud Quick Start Guide gives you step-by-step instructions on how to set up your HiveMQ Cloud account, create clusters, and connect MQTT clients.

Prerequisites

After signing up, you have a running HiveMQ Cloud cluster, that you can use in this example. Now clone this repository into your local IDE.

For using the code examples, you need to install MQTT.js. Execute this command in the terminal of your IDE to install all dependencies.

npm install

Broker credentials

To define the HiveMQ Cloud cluster which should be targeted, you need to fill the placeholders in the code with your host name, username and password. The host name can be found in the Details section of the Overview tab of your cluster. cluster overview

After the cluster is created, add a set of credentials that you can use in this example or future implementations. Use any secure username and password you desire. The username and password are the values used as Client Credentials that you just created. credentials

Code Examples

This example project covers the core functionality of an MQTT client interacting with HiveMQ Cloud. To securely connect the MQTT client with HiveMQ Cloud you need to enable TLS. Use your username and password, to authenticate your MQTT client at HiveMQ Cloud. To connect the client, use the port 8883 that is standard for secure MQTT communication.

const options = {
  username: '<your_username>',
  password: '<your_password>',
  rejectUnauthorized: false
};
const client = mqtt.connect('tls://your_host:port', options);

The code located inside the example.js connects to the configured HiveMQ Cloud Broker. This is a ready-set example that can simply be run after inputting your credentials by executing this command in the terminal of your IDE.

node example.js

The code first subscribes to the topic filter 'messages'.
That means the MQTT client receives all messages that are published to this topic filter.

client.subscribe('messages');

Then the code publishes a message to the same topic.

client.publish('messages', 'Hello, this message was received!');

The callback gets triggered when a message is received and prints it to the console.

client.on('message', function(topic, message) {
  console.log(String.fromCharCode.apply(null, message)); // need to convert the byte array to string
});

There are also callbacks for a successful connection and when an error occurs.

Learn more

If you want to learn more about MQTT, visit the MQTT Essentials guide, that explains the core of MQTT concepts, its features and other essential information. Learn about MQTT.js at the official Github repository.

Contributing

Please see our contributing guidelines and code of conduct.

License

Apache 2.0.

mqtt.js-client-example's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

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