Skip to main content

Server Quickstart (Node.js)

In this quickstart we'll set up a simple server to echo incoming messages using Kanalo. If you don't want to set up a backend right now, you can also skip directly to the Client Quickstart using the default echo pre-configured on your tenant.

The echo server needs to perform three main tasks:

  1. Provide an endpoint to receive the incoming messages
  2. Authenticate the incoming messages
  3. Respond by sending the messages back to their endpoints

1. Install Prerequisites

You'll need Node.js to follow along with this quickstart. Once Node.js is installed you can bootstrap a project by creating a folder and running npm init for an interactive setup.

We'll use express to create the server. We'll use the express-jwt library for validating the authenticity of the request. To install them using npm:

npm i express express-jwt

2. Set up your server

The details of setting up a simple "Hello World" express server can be found in the express docs here. Follow those steps. Once your server is working, we'll modify it to communicate using Kanalo.

2.1 Routing

Kanalo sends PUT requests instead of GET. We'll change line 5 to accommodate this:

const express = require('express')
const app = express()
const port = 3000

app.put('/', (req, res) => {
res.send('Hello World!')
})

app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})

2.2 Auth

Now we'll add the express-jwt middleware to authenticate the incoming requests:

const express = require('express')
const jwt = require('express-jwt')
const app = express()
const port = 3000

const checkJwt = jwt({
secret: 'my-secret',
audience: 'http://hooks.my-url.com',
issuer: 'my-url',
algorithms: ['HS256'],
})

app.use(checkJwt);

app.put('/', (req, res) => {
res.send('Hello World!')
})

app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})

Change the secret, audience, and issuer to the proper values for your setup. This will check the token to ensure data is coming from Kanalo.

2.3 Response

Finally we need the echo itself, replacing the previous "Hello World!" response on our route. We'll also add the JSON parsing middleware provided by express. Modify the put request as follows:

app.put('/', express.json(), (req, res) => {
const { body } = req
const response = body.map((event) => {
return {
method: 'message',
params: {
sockets: [event.socket.id],
message: event.body.message,
},
}
})

res.status(200).json(response)
})

Here we take the contents of the body, which will be an array of incoming messages, and for each one, add a command object to the returned array. This object will tell Kanalo to call the 'message' command to send the data (in this case, the data they sent us) back to the originating socket. We retrieve the id of the originating socket using the socket.id property of the incoming event payload.

The full file will now look like so:

const express = require('express')
const jwt = require('express-jwt')
const app = express()
const port = 3000

const checkJwt = jwt({
secret: 'my-secret',
audience: 'http://hooks.my-url.com',
issuer: 'my-url',
algorithms: ['HS256'],
})

app.use(checkJwt);

app.put('/', express.json(), (req, res) => {
const { body } = req
const response = body.map((event) => {
return {
method: 'messageSocket',
params: {
sockets: [event.socket.id],
message: event.body.message,
},
}
})

res.status(200).json(response)
})

app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})

3. Hosting the server

Your server will need to be contactable by the wider internet in order for Kanalo to send messages to it. You can use a hosting service, or host the server locally and use a tunneling service such as ngrok to access it.

Now that the server is running, we can move on to setting up Kanalo to transmit to it.