title: Websocket on("message") not working
body: <p>I am creating a node.js app and running a WebSocket using both a server.js file and a routes.js file. I am trying to use the routes.js file to send a message within a certain page and eventually going to target the socket message to specific websocket clients. For now I am having trouble getting the on("message") function to work.</p>
  
  <p>From the server.js file:</p>
  
  <pre><code>require('./app/routes.js')(app, passport, wss); // load our routes and pass in our app and fully configured passport
  
  wss.on('connection', function connection(ws, req) {
              console.log('Client connected');
              ws.on('message', function incoming(message){
                  console.log("Incoming message: %s", message);
              })
              ws.on('close', () =&gt; console.log('Client disconnected'));
  })
          ;
  </code></pre>
  
  <p>From the routes.js file:</p>
  
  <pre><code>app.post('/allbooks', function(
  
  req,res)
  {
      wss.clients.forEach((client) =&gt; {
      var arrayToSend = JSON.stringify([req.body.bookid, [], req.body.userSays]);
          console.log("Printing within routes.js");
          console.log(arrayToSend);
          client.send(arrayToSend);
      });
      res.redirect('/allbooks');    
  });
  </code></pre>
  
  <p>When I send a message, I see "Printing within routes.js" in the log, but I don't see the "Incoming message" part from the server.js part. Why not? </p>
  
  <p>PS Here is the scripts.js file from the page I am running:</p>
  
  <pre><code>var HOST = location.origin.replace(/^http/, 'ws')
    var ws = new WebSocket(HOST);
    ws.onmessage = function (event) {
      eventJSON = JSON.parse(event.data);
        if ($("#"+eventJSON[0]+"-request").length &gt; 0){
          $("#"+eventJSON[0]+"-request").html("Message sent");
        }
      } 
  </code></pre>