UNPKG

snips-sam

Version:

The Snips Assistant Manager

47 lines (33 loc) 2.27 kB
--- layout: article title: "Listening to intents over MQTT using Javascript" permalink: /tutorials/mqtt-js toc: The Hermes protocol|Listening to MQTT events|Handling intents --- In this tutorial, you will learn how to create a standalone Javascript application that listens to various events coming from the Snips Platform, in the form of [MQTT](http://mqtt.org/) messages. # The Hermes protocol The [Snips Hermes Protocol](https://github.com/snipsco/snips-platform-documentation/wiki/6.--Miscellaneous#hermes-protocol) is the set of messages and exchange of messages between the various components of the Snips Platform. The messages are MQTT messages. The figure below describes the flow of messages during a simple voice interaction session. <img src="{{ site.baseurl }}/images/Snips-Hermes.png" /> You will most likely be interested in the following messages: * `hermes/hotword/detected`: when a wakework is detected, this message will be sent, and Snips will listening to your voice command. You can for instance use this message to play a little tone, or start a led animation, indicating the start of a listening session * `hermes/asr/textCaptured`: when listening, Snips will transcribe your query into text, and after a small period of silence, it will stop listening, and send this message. You may use this to play another tone, or stop a led animation, indicating end of listening * `hermes/intent/intentName`: after text has been captured, the NLU module will process the text and transform it into an intent, which is the final representation of your query that you can use as an actionable item. This is where you want to put your intent handler code # Listening to MQTT events When launched, Snips starts an MQTT broker that it uses to broadcast the above MQTT messages. We can connect to this broker using the [MQTT.js](https://github.com/mqttjs/MQTT.js) Node: ```sh $ npm install -g mqtt ``` The connection code looks as follows: ```javascript var mqtt = require('mqtt') var client = mqtt.connect('SNIPS_HOSTNAME') client.on('connect', function () { client.subscribe('hermes') }) client.on('message', function (topic, message) { // message is Buffer console.log(message.toString()) client.end() }) ``` # Handling intents