title: Javascript Websocket server message broadcast to clients
body: <p>I am trying to create a dummy websocket server in javascript to send some message to my android client app. The messages will be injected to the server using a html page( javascript ), which will further be passed on to the android client. I am able to connect these two clients (web and android) individually with the server, however, unable to achieve the flow I want, i.e. Web based javascript sends message to running Nodejs websocket server, which broadcast this message to the android client.</p>
  
  <p>This is the code I am using for server side</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var WebSocketServer = require("ws").Server;
  var http = require("http");
  var express = require("express");
  var port = 2001;
  
  var app = express();
  app.use(express.static(__dirname + "/../"));
  app.get('/someGetRequest', function(req, res, next) {
    console.log('receiving get request');
  });
  app.post('/somePostRequest', function(req, res, next) {
    console.log('receiving post request');
  });
  app.listen(80); //port 80 need to run as root
  
  console.log("app listening on %d ", 80);
  
  var server = http.createServer(app);
  server.listen(port);
  
  console.log("http server listening on %d", port);
  
  var userId;
  var wss = new WebSocketServer({
    server: server
  });
  wss.on("connection", function(ws) {
    console.info("websocket connection open");
  
    var timestamp = new Date().getTime();
    userId = timestamp;
  
    ws.send(JSON.stringify({
      msgType: "onOpenConnection",
      msg: {
        connectionId: timestamp
      }
    }));
  
  
    ws.on("message", function(data, flags) {
      console.log("websocket received a message");
      var clientMsg = data;
  
      ws.send(JSON.stringify({
        msg: {
          connectionId: userId
        }
      }));
      console.log(clientMsg);
  
    });
  
    ws.on("close", function() {
      console.log("websocket connection close");
    });
  });
  console.log("websocket server created");</code></pre>
  </div>
  </div>
  </p>
  
  <p>WebClient:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>&lt; script type = "text/javascript" &gt;
    var websocketURL = 'ws://localhost:2001/';
  
  function startWebSocket() {
    try {
      ws = new WebSocket(websocketURL);
    } catch (e) {
      alert("Unable to connect to webserver")
    }
  }
  
  function sendMessage(text) {
    var message = 'Test message from webclient: ' + text;
    ws.send(message);
    alert(message);
  }
  
  startWebSocket(); &lt; /script&gt;
          
          &lt;button onclick="sendMessage('From button1')"&gt;Button 1&lt;/button &gt; &lt; br &gt;
    &lt; button onclick = "sendMessage('From button2')" &gt; Button 2 &lt; /button&gt;&lt;br&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Android client:</p>
  
  <p>Just using socket class and its method to do further processing</p>
  
  <pre><code>               s = new Socket(HOST, TCP_PORT);
  </code></pre>
  
  <p>Please let me know how I can pass the message generated from the web client to my android client via websocket server.</p>
  
  <p>I am using nodejs for websocket server implementation.</p>
  
  <p>Thanks</p>