title: How to connect to a websocket from a different server?
body: <p>I have <strong>server A</strong> "windows 7 Pro" where I installed <a href="https://nodejs.org/download/" rel="nofollow">node.js</a> and ran it using this command <code>node ws_server.js</code> <a href="http://cjihrig.com/blog/websockets-in-node-js-0-8-6-for-windows-7/" rel="nofollow">following the instructions here</a></p>
  
  <p>From <strong>server B</strong> "Windows Server 2008 R2" running Apache 2.4/php 5.6.13 I want to connect to the <code>ws_server</code> on <strong>Server A</strong>.</p>
  
  <p>on **Server B* I have a script called <code>websocket.php</code> with the code below</p>
  
  <pre><code>&lt;script&gt;
  
          $(function() {
  
              var WebSocketClient = require('websocket').client;
  
              var client = new WebSocketClient();
  
              client.on('connectFailed', function(error) {
                  console.log('Connect Error: ' + error.toString());
              });
  
              client.on('connect', function(connection) {
                  console.log('WebSocket Client Connected');
                  connection.on('error', function(error) {
                      console.log("Connection Error: " + error.toString());
                  });
                  connection.on('close', function() {
                      console.log('echo-protocol Connection Closed');
                  });
                  connection.on('message', function(message) {
                      if (message.type === 'utf8') {
                          console.log("Received: '" + message.utf8Data + "'");
                      }
                  });
  
                  function sendNumber() {
                      if (connection.connected) {
                          var number = Math.round(Math.random() * 0xFFFFFF);
                          connection.sendUTF(number.toString());
                          setTimeout(sendNumber, 1000);
                      }
                  }
                  sendNumber();
              });
  
              client.connect('ws://ServerA:8080/', 'echo-protocol');
          });
  
      &lt;/script&gt;
  </code></pre>
  
  <p>But for some reason I get this error in the console. </p>
  
  <blockquote>
    <p>ReferenceError: require is not defined</p>
  </blockquote>
  
  <p>Do I need to take files from the nodejs folder from server A and include it in the client script? if so which files do I need to include?</p>
  
  <p><strong>Note:</strong> I have included jQuery files as well</p>
  
  <p><strong>EDITED</strong></p>
  
  <p>this is my client code </p>
  
  <pre><code>       &lt;script&gt;
              "use strict";
              // Initialize everything when the window finishes loading
              window.addEventListener("load", function(event) {
                var status = document.getElementById("status");
                var url = document.getElementById("url");
                var open = document.getElementById("open");
                var close = document.getElementById("close");
                var send = document.getElementById("send");
                var text = document.getElementById("text");
                var message = document.getElementById("message");
                var socket;
  
                status.textContent = "Not Connected";
                url.value = "ws://serverB:8080";
                close.disabled = true;
                send.disabled = true;
  
                // Create a new connection when the Connect button is clicked
                open.addEventListener("click", function(event) {
                  open.disabled = true;
                  socket = new WebSocket(url.value, "echo-protocol");
  
                  socket.addEventListener("open", function(event) {
                    close.disabled = false;
                    send.disabled = false;
                    status.textContent = "Connected";
                  });
  
                  // Display messages received from the server
                  socket.addEventListener("message", function(event) {
                    message.textContent = "Server Says: " + event.data;
                  });
  
                  // Display any errors that occur
                  socket.addEventListener("error", function(event) {
                    message.textContent = "Error: " + event;
                  });
  
                  socket.addEventListener("close", function(event) {
                    open.disabled = false;
                    status.textContent = "Not Connected";
                  });
                });
  
                // Close the connection when the Disconnect button is clicked
                close.addEventListener("click", function(event) {
                  close.disabled = true;
                  send.disabled = true;
                  message.textContent = "";
                  socket.close();
                });
  
                // Send text to the server when the Send button is clicked
                send.addEventListener("click", function(event) {
                  socket.send(text.value);
                  text.value = "";
                });
              });
    &lt;/script&gt;
  </code></pre>