Search code examples
javascriptnode.jswebsocketservervonage

How to have multiple websocket connections using Nexmo


I implemented the nexmo provided example code (below) into my server. However, I'm running into an issue where if 2 callers ping my server, the second caller's binary data also streams into the same websocket endpoint; thus resulting in two binary streams into the same websocket endpoint. How could I implement nexmo so that each caller has their own websocket to connected to an agent? I believe socket.io is the solution, however I'm unfamiliar with it.

const express = require('express')
const app = express()
var bodyParser = require('body-parser');
app.use(bodyParser.json());
var expressWs = require('express-ws')(app);
var isBuffer = require('is-buffer')
var header = require("waveheader");
var fs = require('fs');
var file;



//Serve a Main Page
app.get('/', function(req, res) {
    res.send("Node Websocket");
});


//Serve the NCCO on the /ncco answer URL
app.get('/ncco', function(req, res) {

    var ncco = require('./ncco.json');
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify(ncco), 'utf-8');
});


//Log the Events
app.post('/event', function(req, res) {
    console.log(req.body);
    res.send("ok");
});

// Handle the Websocket
app.ws('/socket', function(ws, req) {
    var rawarray = []; 
    console.log("Websocket Connected")
    ws.on('message', function(msg) {
     if (isBuffer(msg)) {
             rawarray.push(msg);
     }
     else {
         console.log(msg); 
     }
    });
    ws.on('close', function(){
      console.log("Websocket Closed")
      file = fs.createWriteStream('./output.wav');
      file.write(header(16000 * rawarray.length/50 * 2,{
                        sampleRate: 16000,
                        channels: 1,
                        bitDepth: 16}));
      rawarray.forEach(function(data){
          file.write(data);
      });
  })
});

app.listen(8000, () => console.log('App listening on port 8000!'))

Solution

  • It should work with your current implementation, you'd need to do a few changes. So instead of every WebSocket going in the same endpoint, and writing to the same output file, you'd need to stream each WebSocket on a separate route, and save each output to different files. Change your socket route to look like this:

    app.ws('/socket/:identifier', function(ws, req) {
        var rawarray = []; 
        console.log("Websocket Connected")
        ws.on('message', function(msg) {
         if (isBuffer(msg)) {
                 rawarray.push(msg);
         }
         else {
             console.log(msg); 
         }
        });
        ws.on('close', function(){
          console.log("Websocket Closed")
          file = fs.createWriteStream('./' + req.params.identifier + '.wav');
          file.write(header(16000 * rawarray.length/50 * 2,{
                            sampleRate: 16000,
                            channels: 1,
                            bitDepth: 16}));
          rawarray.forEach(function(data){
              file.write(data);
          });
      })
    });
    

    To make this work, you'd need to change your NCCO as well, instead of a static file read from disk, generate it in the route. I don't know what exactly you have in the file, but you can use it as is and change the socket connection bit to something like:

    app.get('/ncco', function(req, res) {
    
      var ncco = [
        {
          "action": "talk",
          "text": "Please wait while we connect you"
        },
        {
          "action": "connect",
          "eventUrl": [
            "https://yourserver.com/event"
          ],
          "from": "YOUR_NEXMO_NUMBER",
          "endpoint": [
            {
              "type": "websocket",
              "uri": "ws://yourserver.com/socket/" + req.query.from,
              "content-type": "audio/l16;rate=16000"
            }
          ]
        }
      ]
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify(ncco), 'utf-8');
    });