Simple, EventEmitter API for WebSockets (browser)
- super simple API for working with WebSockets in the browser
- supports text and binary data
npm install simple-websocket
var SimpleWebsocket = require'simple-websocket'var socket = 'ws://echo.websocket.org'socketon'ready'// socket is connected!socketsend'sup!'socketon'message'console.log'got message: ' + data
Note: If you're NOT using browserify, then use the standalone
file included in this repo. This exports a
SimpleWebsocket function on the
Create a new WebSocket connection.
opts is specified, then the default options (shown below) will be overridden.
The options do the following:
reconnect- If websocket encounters an error, reconnect after this timeout (in milliseconds). Set to
falseto disable automatic reconnect on error.
Send text/binary data to the remote socket.
data can be any of several types:
Buffer (see buffer), TypedArrayView (Uint8Array, etc.), or ArrayBuffer.
Note: this method should not be called until the
sockt.on('ready') event has fired.
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.
Fired when the websocket connection is ready to use.
Received a message from the websocket server.
data will be either a
String or a
Buffer/Uint8Array (see buffer).
Called when the websocket connection has closed.
err is an
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.
err is an
Fired when an error occurs but an auto-reconnect will be attempted. Thus, it's only a
warning, not a full-fledged
- 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 ]
MIT. Copyright (c) Feross Aboukhadijeh.