title: nodejs websockets subscribe to multiple channels
body: <p>The end goal is for my nodejs program to subscribe to multiple channels supplying real-time financial information and processing the streaming data.</p>
  
  <p>In python it is possible to subscribe to multiple channels using the functools/partial and websocket. This allows one python program to subscribe to multiple channels. In my case I am trying to do the same with nodejs/javascript.</p>
  
  <p><a href="https://stackoverflow.com/questions/45645865/python-websockets-subscribe-to-multiple-channels">Python websockets, subscribe to multiple channels</a></p>
  
  <p>Is it possible to do something similar in nodejs with a combination of wesocket and some other package? </p>
  
  <p>What I have below works but it outputs the streams from three different channels in one output which is difficult to manage.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>/*jshint esversion: 6 */
  
  var sys = require('util');
  
  const WebSocket = require('ws')
  const wss = new WebSocket('wss://api.bitfinex.com/ws/')
  
  wss.onopen = () =&gt; {
    console.log('Subscribing: BTCUSD');
    wss.send(JSON.stringify(
      {
        "event":"subscribe",
        "channel":"book",
        "pair":"BTCUSD",
        "prec":"P0",
        "freq":"F1"
      }
    ));
    console.log('Subscribing: LTCUSD');
    wss.send(JSON.stringify(
      {
        "event":"subscribe",
        "channel":"book",
        "pair":"LTCUSD",
        "prec":"P0",
        "freq":"F1"
      }
    ));
    console.log('Subscribing: LTCBTC');
    wss.send(JSON.stringify(
      {
        "event":"subscribe",
        "channel":"book",
        "pair":"LTCBTC",
        "prec":"P0",
              "freq":"F1"
      }
    ));
    console.log('Subscribing: ETHUSD');
    wss.send(JSON.stringify(
      {
        "event":"subscribe",
        "channel":"book",
        "pair":"ETHUSD",
        "prec":"P0",
              "freq":"F1"
      }
    ));
  }
  
  wss.onmessage = (msg) =&gt; {
    var response = JSON.parse(msg.data);
    var head = response["event"];
    var head_body = response[1];
    
    if ( head == "subscribed" ) {
      console.log("channelID = ", response["chanId"], " currency = ", response["pair"]);
    } else {
      console.log(msg.data);
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Appreciate the guidance on this. Am also looking at socket.io. Maybe that can do it?</p>