The Echolo IoT Service is an IoT Message broker that allows devices to communicate to each other or to the cloud easily. This service uses a pub/sub (Publish & Subscribe) model allowing a Hub to connect and send or receive messages on any allowed Topics.

<aside> 🗒️ NOTE: All our examples will use the Echolo NPM Library but we support any mqtt library. When using a 3rd party library you will still need to authenticate with the service using your hubId and key. Often times this is labeled as username and password.

</aside>

Open a connection to the IoT Service

First, we need to open the client and authenticate using our hubId and key.

let iot = Echolo.IoT({ "hubId": "myHubId", "key": "xxxx-xxxx-xxxx-xxxxx-xxxxxx-xxxxxxx" })

Subscribe to the Hub's Topic

When a hub is created, it gets its own topic that it can both read and write to. Review Topics and Hub for more information.

For this example lets say our data is as follows:

appId hubId key
12345678-1234-1234-1234-098765431 EcholoHub001 abc123-bca321-1234-0987-a0b9c9f9e9

As it shows in the Topics guide we form the topic path like so: hub/appId/hubId using the data above it comes to be: hub/12345678-1234-1234-1234-098765431/EcholoHub001

iot.on('connect', function() {
  console.log('connected')
  iot.subscribe('hub/12345678-1234-1234-1234-098765431/EcholoHub001')
})

After we subscribe to the topic we then need to watch for an event called message.

iot.on('message', (topic, message) => {
  console.log(JSON.parse(message))
})

Publish a message to the same Topic

Now that we have opened a subscription to the hub Topic. Let's send it a message, again we will be using the same topic above hub/12345678-1234-1234-1234-098765431/EcholoHub001.

let data = { myKey: 'My Value', someInt: 123 }

iot.publish('hub/12345678-1234-1234-1234-098765431/EcholoHub001', JSON.stringify(data))

Related articles