Hello TurtleQueue in JS

JS developer

This tutorial explains how to get started with TurtleQueue in Node.js and/or in the browser.

In a hurry? Just clone the demo project which contains the final result:

git clone https://github.com/turtlequeue/quickstart-js

Otherwise follow this guide, which will guide you through sending messages in #channels as well as geolocalized.

A bit of boilerplate

First, import the library:

const turtlequeue = require('turtlequeue');
const websocket = require('websocket') (1)
1 The library import for the websocket package is only required on nodeJS

We then read the environment variables from the environment (adapt to your build process in the browser). These credentials can be found on your dashboard: https://turtlequeue.com/security.html

const API_KEY = process.env.TURTLEQUEUE_API_KEY       (2)

if (!USER_TOKEN) {
  console.log('Missing USER_TOKEN')              (3)

if (!API_KEY) {
  console.log('Missing API_KEY')                 (4)
1 a UserToken uniquely identifies an account
2 an apiKey contains the necessary permissions for publishing and subscribing.
3 if you see this message you need to "export USER_TOKEN=XXX" where "XXX" is the secret value from https://turtlequeue.com/security.html
4 if you see this message you need to "export API_KEY=XXX" where "XXX" is the secret value from https://turtlequeue.com/security.html

Time to instantiate a new turtlequeue instance.

const q = turtlequeue({
  host: 'turtlequeue.com',   (1)
  type: 'ws',                (2)
  protocol: 'https',         (3)
1 This is only useful for when turtlequeue is deployed in hosted environments
2 Only 'ws' (websockets, the default) is supported for now
3 always prefer 'https' - this is useful in hosted environments

We now have q holding an instance that will be able to connect. No HTTP or WebSocket connection has been opened up yet, we need to explicitly trigger it.

So, let’s set up listeners to the various connection events. While not strictly necessary, it is a good practice and can help speed up debugging down the line.

q.on('ready', () => {             (1)
   console.log('TurtleQueue ready')

   setTimeout(publish, 2000)      (2)
    subscribe()                    (3)

turtlequeue.on('error', evt => {

turtlequeue.on('disconnect', evt => {
1 the 'ready' event is fired when turtlequeue.com is reachable and the authentication is correct
2 continuously publish every 2 seconds
3 subscribe, need to do it only once. The callback will be fired every 2.5 seconds

Finally, we now connect!

q.connect({ (1)
  userToken: USER_TOKEN,
  apiKey: API_KEY,

setTimeout(() => { (2)
}, 15000)
1 we connect to the server, using our credentials. If the credentials are correct, the 'ready' event will be fired. Otherwise, the 'error' event will be fired.
2 after 15 seconds running, we trigger a disconnect. Once done, the `disconnect`event will be triggered

We are now connected and ready to go! The connection count on the dashboard should also increasem as a confirmation that you are connected!