Package Exports
- simple-websocket
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (simple-websocket) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
simple-websocket

Simple, EventEmitter API for WebSockets
features
- super simple API for working with WebSockets in the browser
- Uses
EventEmitter
interface - supports text and binary data
This module works in the browser with browserify, and it's used by WebTorrent!
install
npm install simple-websocket
usage
var SimpleWebsocket = require('simple-websocket')
var socket = new SimpleWebsocket('ws://echo.websocket.org')
socket.on('connect', function () {
// socket is connected!
socket.send('sup!')
})
socket.on('data', function (data) {
console.log('got message: ' + data)
})
Note: If you're NOT using browserify, then use the standalone simplewebsocket.bundle.js
file included in this repo. This exports a SimpleWebsocket
function on the window
.
api
socket = new SimpleWebsocket([opts])
Create a new WebSocket connection.
If opts
is specified, then the default options (shown below) will be overridden.
{
reconnect: 5000
}
The options do the following:
reconnect
- If websocket encounters an error, reconnect after this timeout (in milliseconds). Set tofalse
to disable automatic reconnect on error.
socket.send(data)
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).
Other data types will be transformed with JSON.stringify
before sending. This is handy
for sending object literals across like this: socket.send({ type: 'data', data: 'hi' })
.
Note: If this method is called before the socket.on('connect')
event has fired, then
data will be buffered.
socket.destroy([onclose])
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.
events
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).
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. If the reconnect
option is set to something truthy (defaults to 5000
), then this event will never get emitted because the socket will automatically reconnect on error.
socket.on('warning', function (err) {})
err
is an Error
object.
Fired when an error occurs but an auto-reconnect will be attempted. Thus, it's only a warning
, not a full-fledged error
.
real-world applications that use simple-websocket
- StudyNotes - Helping students learn faster and better
- instant.io - Secure, anonymous, streaming file transfer
- lxjs-chat - Omegle chat clone
- [ your application here - send a PR ]
license
MIT. Copyright (c) Feross Aboukhadijeh.