UNPKG

3.25 kBMarkdownView Raw
1# simple-websocket [![travis](https://img.shields.io/travis/feross/simple-websocket/master.svg)](https://travis-ci.org/feross/simple-websocket) [![npm](https://img.shields.io/npm/v/simple-websocket.svg)](https://npmjs.org/package/simple-websocket)
2
3#### Simple, EventEmitter API for WebSockets
4
5[![Sauce Test Status](https://saucelabs.com/browser-matrix/simple-websocket.svg)](https://saucelabs.com/u/simple-websocket)
6
7## features
8
9- **super simple** API for working with WebSockets in the browser
10- supports **text and binary data**
11- node.js [duplex stream](http://nodejs.org/api/stream.html) interface
12
13This module works in the browser with [browserify](http://browserify.org/), and it's used by [WebTorrent](http://webtorrent.io)!
14
15## install
16
17```
18npm install simple-websocket
19```
20
21## usage
22
23```js
24var SimpleWebsocket = require('simple-websocket')
25
26var socket = new SimpleWebsocket('ws://echo.websocket.org')
27socket.on('connect', function () {
28 // socket is connected!
29 socket.send('sup!')
30})
31
32socket.on('data', function (data) {
33 console.log('got message: ' + data)
34})
35```
36
37Note: If you're **NOT** using browserify, then use the standalone `simplewebsocket.min.js`
38file included in this repo. This exports a `SimpleWebsocket` function on the `window`.
39
40## api
41
42### `socket = new SimpleWebsocket([opts])`
43
44Create a new WebSocket connection.
45
46If `opts` is specified, then it will be passed through to the underlying superclass, `stream.Duplex`.
47
48### `socket.send(data)`
49
50Send text/binary data to the WebSocket server. `data` can be any of several types:
51`String`, `Buffer` (see [buffer](https://github.com/feross/buffer)), `TypedArrayView`
52(`Uint8Array`, etc.), `ArrayBuffer`, or `Blob` (in browsers that support it).
53
54Note: If this method is called before the `socket.on('connect')` event has fired, then
55data will be buffered.
56
57### `socket.destroy([onclose])`
58
59Destroy and cleanup this websocket connection.
60
61If the optional `onclose` paramter is passed, then it will be registered as a listener on the 'close' event.
62
63### `Socket.WEBSOCKET_SUPPORT`
64
65Detect WebSocket support in the javascript environment.
66
67```js
68var Socket = require('simple-websocket')
69
70if (Socket.WEBSOCKET_SUPPORT) {
71 // websocket support!
72} else {
73 // fallback
74}
75```
76
77
78## events
79
80### `socket.on('connect', function () {})`
81
82Fired when the websocket connection is ready to use.
83
84### `socket.on('data', function (data) {})`
85
86Received a message from the websocket server.
87
88`data` will be either a `String` or a `Buffer/Uint8Array` (see [buffer](https://github.com/feross/buffer)).
89JSON strings will be parsed and the resulting `Object` emitted.
90
91### `socket.on('close', function () {})`
92
93Called when the websocket connection has closed.
94
95### `socket.on('error', function (err) {})`
96
97`err` is an `Error` object.
98
99Fired when a fatal error occurs.
100
101## real-world applications that use simple-websocket
102
103- [StudyNotes](http://www.apstudynotes.org) - Helping students learn faster and better
104- [instant.io](https://github.com/feross/instant.io) - Secure, anonymous, streaming file transfer
105- [lxjs-chat](https://github.com/feross/lxjs-chat) - Omegle chat clone
106- \[ your application here - send a PR \]
107
108## license
109
110MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org).