title: How to use websocket npm library in browser?
body: <p>I started learning node.js. I want to make a chat application using websockets. In console it's working fine. But in browser i'm getting </p>
  
  <blockquote>
    <p>Uncaught ReferenceError: require is not defined</p>
  </blockquote>
  
  <p>I googled and saw this <a href="https://stackoverflow.com/a/19059825/2689414">answer</a>. I want to try browserify but it's not working properly. Here is my js file</p>
  
  <p><strong>client.js</strong>:</p>
  
  <pre><code>var WebSocket = require('websocket');
  var connector;
  var WebSocketClient = WebSocket.client;
  var client = new WebSocketClient();
  
  
  client.on('connectFailed', function (error) {
      console.log('Connect Error: ' + error.toString());
      setTimeout(self.connect, 2000);
  });
  
  client.on('connect', function (connection) {
      connector = connection;
      console.log('WebSocket Client Connected');
      connection.on('error', function (error) {
          errorStr = error.toString();
          console.log("Connection Error: " + errorStr);
      });
      connection.on('close', function () {
          console.log('echo-protocol Connection Closed');
      });
      connection.on('message', function (message) {
          if (message.type === 'utf8') {
              console.log("Received: '" + message.utf8Data + "'");
          }
      });
      connection.sendUTF("Hello");
      sender("Working");
  
  });
  
  client.on('error', function (err) {
      console.log("It's an Err " + err.toString());
  });
  
  function connect() {
      client.connect('ws://localhost:8765/', 'echo-protocol');
  }
  
  function sender(str) {
      connector.sendUTF(str);
  }
  connect();
  </code></pre>
  
  <p>Here is the problem. When I run bunddle.js.</p>
  
  <pre><code>$ browserify  client.js &gt; bunddle.js
  $ node bunddle.js
  /home/user/udb/sgg/node/chat/bundle.js:5
  var client = new WebSocketClient();
               ^
  TypeError: undefined is not a function
  </code></pre>