node package manager


Simple, EventEmitter API for WebSockets (browser)

simple-websocket travis npm

Simple, EventEmitter API for WebSockets

Sauce Test Status


  • super simple API for working with WebSockets in the browser
  • supports text and binary data
  • node.js duplex stream interface

This module works in the browser with browserify, and it's used by WebTorrent!


npm install simple-websocket


var SimpleWebsocket = require('simple-websocket')
var socket = new SimpleWebsocket('ws://')
socket.on('connect', function () {
  // socket is connected! 
socket.on('data', function (data) {
  console.log('got message: ' + data)

Note: If you're NOT using browserify, then use the standalone simplewebsocket.min.js file included in this repo. This exports a SimpleWebsocket function on the window.


socket = new SimpleWebsocket([opts])

Create a new WebSocket connection.

If opts is specified, then it will be passed through to the underlying superclass, stream.Duplex.


Send text/binary data to the WebSocket server. data can be any of several types: String, Buffer (see buffer), TypedArrayView (Uint8Array, etc.), ArrayBuffer, or Blob (in browsers that support it).

Note: If this method is called before the socket.on('connect') event has fired, then data will be buffered.


Destroy and cleanup this websocket connection.

If the optional onclose paramter is passed, then it will be registered as a listener on the 'close' event.


Detect WebSocket support in the javascript environment.

var Socket = require('simple-websocket')
  // websocket support! 
} else {
  // fallback 


socket.on('connect', function () {})

Fired when the websocket connection is ready to use.

socket.on('data', function (data) {})

Received a message from the websocket server.

data will be either a String or a Buffer/Uint8Array (see buffer). JSON strings will be parsed and the resulting Object emitted.

socket.on('close', function () {})

Called when the websocket connection has closed.

socket.on('error', function (err) {})

err is an Error object.

Fired when a fatal error occurs.

real-world applications that use simple-websocket

  • StudyNotes - Helping students learn faster and better
  • - Secure, anonymous, streaming file transfer
  • lxjs-chat - Omegle chat clone
  • [ your application here - send a PR ]


MIT. Copyright (c) Feross Aboukhadijeh.