# Connect to deployments with Node.js SDK

In this tutorial, you will learn how to use Node.js MQTT.js client to connect to EMQ X Cloud deployment.

# Preparation

  • Already Create Deployment in EMQ X Cloud , and the deployment status is running

  • Install Node.js and NPM

  • Install dependencies

    npm install
    // or use yarn
    yarn
    
  • For free trial and shared deployment EMQ X Cloud, it provides TLS/SSL connection authentication by default, and you also need to download root certificate (opens new window) For TLS/SSL connection.

# Example Code

Get Sample Source Code (opens new window)

  • View command line help

    node index.js --help
    
  • Connect to the deployment and subscribe to messages with the MQTT protocol

    node index.js
    or
    node index.js -p mqtt
    
  • Connect to the deployment and subscribe to messages with the Websocket protocol

    node index.js -p ws
    
  • Connect to deployment and subscribe to messages with MQTT over TLS/SSL protocol

    node index.js -p mqtts
    
  • Connect to deployment and subscribe to messages with Websocket over TLS/SSL protocol

    node index.js -p wss