4 [![Build Status](https://github.com/socketio/engine.io-client/workflows/CI/badge.svg)](https://github.com/socketio/engine.io-client/actions)
5 [![NPM version](https://badge.fury.io/js/engine.io-client.svg)](http://badge.fury.io/js/engine.io-client)
7 This is the client for [Engine.IO](http://github.com/socketio/engine.io),
8 the implementation of transport-based cross-browser/cross-device
9 bi-directional communication layer for [Socket.IO](http://github.com/socketio/socket.io).
15 You can find an `engine.io.js` file in this repository, which is a
16 standalone build you can use as follows:
19 <script src="/path/to/engine.io.js"></script>
22 var socket = eio('ws://localhost');
23 socket.on('open', function(){
24 socket.on('message', function(data){});
25 socket.on('close', function(){});
32 Engine.IO is a commonjs module, which means you can include it by using
33 `require` on the browser and package using [browserify](http://browserify.org/):
35 1. install the client package
38 $ npm install engine.io-client
41 1. write your app code
44 var socket = require('engine.io-client')('ws://localhost');
45 socket.on('open', function(){
46 socket.on('message', function(data){});
47 socket.on('close', function(){});
51 1. build your app bundle
54 $ browserify app.js > bundle.js
57 1. include on your page
60 <script src="/path/to/bundle.js"></script>
63 ### Sending and receiving binary
66 <script src="/path/to/engine.io.js"></script>
68 var socket = new eio.Socket('ws://localhost/');
69 socket.binaryType = 'blob';
70 socket.on('open', function () {
71 socket.send(new Int8Array(5));
72 socket.on('message', function(blob){});
73 socket.on('close', function(){ });
80 Add `engine.io-client` to your `package.json` and then:
83 var socket = require('engine.io-client')('ws://localhost');
84 socket.on('open', function(){
85 socket.on('message', function(data){});
86 socket.on('close', function(){});
90 ### Node.js with certificates
93 key: fs.readFileSync('test/fixtures/client.key'),
94 cert: fs.readFileSync('test/fixtures/client.crt'),
95 ca: fs.readFileSync('test/fixtures/ca.crt')
98 var socket = require('engine.io-client')('ws://localhost', opts);
99 socket.on('open', function(){
100 socket.on('message', function(data){});
101 socket.on('close', function(){});
105 ### Node.js with extraHeaders
109 'X-Custom-Header-For-My-Project': 'my-secret-access-token',
110 'Cookie': 'user_session=NI2JlCKF90aE0sJZD9ZzujtdsUqNYSBYxzlTsvdSUe35ZzdtVRGqYFr0kdGxbfc5gUOkR9RGp20GVKza; path=/; expires=Tue, 07-Apr-2015 18:18:08 GMT; secure; HttpOnly'
114 var socket = require('engine.io-client')('ws://localhost', opts);
115 socket.on('open', function(){
116 socket.on('message', function(data){});
117 socket.on('close', function(){});
124 - Runs on browser and node.js seamlessly
125 - Transports are independent of `Engine`
128 - Runs inside HTML5 WebWorker
129 - Can send and receive binary data
130 - Receives as ArrayBuffer or Blob when in browser, and Buffer or ArrayBuffer
132 - When XHR2 or WebSockets are used, binary is emitted directly. Otherwise
133 binary is encoded into base64 strings, and decoded when binary types are
135 - With browsers that don't support ArrayBuffer, an object { base64: true,
136 data: dataAsBase64String } is emitted on the `message` event.
142 The client class. Mixes in [Emitter](http://github.com/component/emitter).
143 Exposed as `eio` in the browser standalone build.
147 - `protocol` _(Number)_: protocol revision number
148 - `binaryType` _(String)_ : can be set to 'arraybuffer' or 'blob' in browsers,
149 and `buffer` or `arraybuffer` in Node. Blob is only used in browser if it's
155 - Fired upon successful connection.
157 - Fired when data is received from the server.
159 - `String` | `ArrayBuffer`: utf-8 encoded data or ArrayBuffer containing
162 - Fired upon disconnection. In compliance with the WebSocket API spec, this event may be
163 fired even if the `open` event does not occur (i.e. due to connection error or `close()`).
165 - Fired when an error occurs.
167 - Fired upon completing a buffer flush
169 - Fired after `drain` event of transport if writeBuffer is empty
171 - Fired if an error occurs with a transport we're trying to upgrade to.
173 - Fired upon upgrade success, after the new transport is set
175 - Fired upon _flushing_ a ping packet (ie: actual packet write out)
177 - Fired upon receiving a pong packet.
182 - Initializes the client
185 - `Object`: optional, options object
187 - `agent` (`http.Agent`): `http.Agent` to use, defaults to `false` (NodeJS only)
188 - `upgrade` (`Boolean`): defaults to true, whether the client should try
189 to upgrade the transport from long-polling to something better.
190 - `forceJSONP` (`Boolean`): forces JSONP for polling transport.
191 - `jsonp` (`Boolean`): determines whether to use JSONP when
192 necessary for polling. If disabled (by settings to false) an error will
193 be emitted (saying "No transports available") if no other transports
194 are available. If another transport is available for opening a
195 connection (e.g. WebSocket) that transport
196 will be used instead.
197 - `forceBase64` (`Boolean`): forces base 64 encoding for polling transport even when XHR2 responseType is available and WebSocket even if the used standard supports binary.
198 - `enablesXDR` (`Boolean`): enables XDomainRequest for IE8 to avoid loading bar flashing with click sound. default to `false` because XDomainRequest has a flaw of not sending cookie.
199 - `withCredentials` (`Boolean`): defaults to `true`, whether to include credentials (cookies, authorization headers, TLS client certificates, etc.) with cross-origin XHR polling requests.
200 - `timestampRequests` (`Boolean`): whether to add the timestamp with each
201 transport request. Note: polling requests are always stamped unless this
202 option is explicitly set to `false` (`false`)
203 - `timestampParam` (`String`): timestamp parameter (`t`)
204 - `policyPort` (`Number`): port the policy server listens on (`843`)
205 - `path` (`String`): path to connect to, default is `/engine.io`
206 - `transports` (`Array`): a list of transports to try (in order).
207 Defaults to `['polling', 'websocket']`. `Engine`
208 always attempts to connect directly with the first one, provided the
209 feature detection test for it passes.
210 - `transportOptions` (`Object`): hash of options, indexed by transport name, overriding the common options for the given transport
211 - `rememberUpgrade` (`Boolean`): defaults to false.
212 If true and if the previous websocket connection to the server succeeded,
213 the connection attempt will bypass the normal upgrade process and will initially
214 try websocket. A connection attempt following a transport error will use the
215 normal upgrade process. It is recommended you turn this on only when using
216 SSL/TLS connections, or if you know that your network does not block websockets.
217 - `pfx` (`String`|`Buffer`): Certificate, Private key and CA certificates to use for SSL. Can be used in Node.js client environment to manually specify certificate information.
218 - `key` (`String`): Private key to use for SSL. Can be used in Node.js client environment to manually specify certificate information.
219 - `passphrase` (`String`): A string of passphrase for the private key or pfx. Can be used in Node.js client environment to manually specify certificate information.
220 - `cert` (`String`): Public x509 certificate to use. Can be used in Node.js client environment to manually specify certificate information.
221 - `ca` (`String`|`Array`): An authority certificate or array of authority certificates to check the remote host against.. Can be used in Node.js client environment to manually specify certificate information.
222 - `ciphers` (`String`): A string describing the ciphers to use or exclude. Consult the [cipher format list](http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT) for details on the format. Can be used in Node.js client environment to manually specify certificate information.
223 - `rejectUnauthorized` (`Boolean`): If true, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if verification fails. Verification happens at the connection level, before the HTTP request is sent. Can be used in Node.js client environment to manually specify certificate information.
224 - `perMessageDeflate` (`Object|Boolean`): parameters of the WebSocket permessage-deflate extension
225 (see [ws module](https://github.com/einaros/ws) api docs). Set to `false` to disable. (`true`)
226 - `threshold` (`Number`): data is compressed only if the byte size is above this value. This option is ignored on the browser. (`1024`)
227 - `extraHeaders` (`Object`): Headers that will be passed for each request to the server (via xhr-polling and via websockets). These values then can be used during handshake or for special proxies. Can only be used in Node.js client environment.
228 - `onlyBinaryUpgrades` (`Boolean`): whether transport upgrades should be restricted to transports supporting binary data (`false`)
229 - `forceNode` (`Boolean`): Uses NodeJS implementation for websockets - even if there is a native Browser-Websocket available, which is preferred by default over the NodeJS implementation. (This is useful when using hybrid platforms like nw.js or electron) (`false`, NodeJS only)
230 - `localAddress` (`String`): the local IP address to connect to
231 - **Polling-only options**
232 - `requestTimeout` (`Number`): Timeout for xhr-polling requests in milliseconds (`0`)
233 - **Websocket-only options**
234 - `protocols` (`Array`): a list of subprotocols (see [MDN reference](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#Subprotocols))
236 - Sends a message to the server
238 - `String` | `ArrayBuffer` | `ArrayBufferView` | `Blob`: data to send
239 - `Object`: optional, options object
240 - `Function`: optional, callback upon `drain`
242 - `compress` (`Boolean`): whether to compress sending data. This option is ignored and forced to be `true` on the browser. (`true`)
244 - Disconnects the client.
248 The transport class. Private. _Inherits from EventEmitter_.
252 - `poll`: emitted by polling transports upon starting a new request
253 - `pollComplete`: emitted by polling transports upon completing a request
254 - `drain`: emitted by polling transports upon a buffer drain
258 `engine.io-client` is used to test
259 [engine](http://github.com/socketio/engine.io). Running the `engine.io`
260 test suite ensures the client works and vice-versa.
262 Browser tests are run using [zuul](https://github.com/defunctzombie/zuul). You can
263 run the tests locally using the following command.
266 ./node_modules/.bin/zuul --local 8080 -- test/index.js
269 Additionally, `engine.io-client` has a standalone test suite you can run
270 with `make test` which will run node.js and browser tests. You must have zuul setup with
275 The support channels for `engine.io-client` are the same as `socket.io`:
276 - irc.freenode.net **#socket.io**
277 - [Google Groups](http://groups.google.com/group/socket_io)
278 - [Website](http://socket.io)
282 To contribute patches, run tests or benchmarks, make sure to clone the
286 git clone git://github.com/socketio/engine.io-client.git
296 See the `Tests` section above for how to run tests before submitting any patches.
300 MIT - Copyright (c) 2014 Automattic, Inc.