wsEngine: require('eiows').Server // requires having eiows as dependency
});
server.on('connection', /* ... */);
```
#### Server
The main server/manager. _Inherits from EventEmitter_.
##### Events
-`connection`
- Fired when a new connection is established.
- **Arguments**
-`Socket`: a Socket object
-`initial_headers`
- Fired on the first request of the connection, before writing the response headers
- **Arguments**
-`headers` (`Object`): a hash of headers
-`req` (`http.IncomingMessage`): the request
-`headers`
- Fired on the all requests of the connection, before writing the response headers
- **Arguments**
-`headers` (`Object`): a hash of headers
-`req` (`http.IncomingMessage`): the request
-`connection_error`
- Fired when an error occurs when establishing the connection.
- **Arguments**
-`error`: an object with following properties:
-`req` (`http.IncomingMessage`): the request that was dropped
-`code` (`Number`): one of `Server.errors`
-`message` (`string`): one of `Server.errorMessages`
-`context` (`Object`): extra info about the error
| Code | Message |
| ---- | ------- |
| 0 | "Transport unknown"
| 1 | "Session ID unknown"
| 2 | "Bad handshake method"
| 3 | "Bad request"
| 4 | "Forbidden"
| 5 | "Unsupported protocol version"
##### Properties
**Important**: if you plan to use Engine.IO in a scalable way, please
keep in mind the properties below will only reflect the clients connected
to a single process.
-`clients`_(Object)_: hash of connected clients by id.
-`clientsCount`_(Number)_: number of connected clients.
##### Methods
- **constructor**
- Initializes the server
- **Parameters**
-`Object`: optional, options object
- **Options**
-`pingTimeout` (`Number`): how many ms without a pong packet to
consider the connection closed (`20000`)
-`pingInterval` (`Number`): how many ms before sending a new ping
packet (`25000`)
-`upgradeTimeout` (`Number`): how many ms before an uncompleted transport upgrade is cancelled (`10000`)
-`maxHttpBufferSize` (`Number`): how many bytes or characters a message
can be, before closing the session (to avoid DoS). Default
value is `1E6`.
-`allowRequest` (`Function`): A function that receives a given handshake
or upgrade request as its first parameter, and can decide whether to
continue or not. The second argument is a function that needs to be
called with the decided information: `fn(err, success)`, where
`success` is a boolean value where false means that the request is
rejected, and err is an error code.
-`transports` (`<Array> String`): transports to allow connections
to (`['polling', 'websocket']`)
-`allowUpgrades` (`Boolean`): whether to allow transport upgrades
(`true`)
-`perMessageDeflate` (`Object|Boolean`): parameters of the WebSocket permessage-deflate extension
(see [ws module](https://github.com/einaros/ws) api docs). Set to `true` to enable. (defaults to `false`)
-`threshold` (`Number`): data is compressed only if the byte size is above this value (`1024`)
-`httpCompression` (`Object|Boolean`): parameters of the http compression for the polling transports
(see [zlib](http://nodejs.org/api/zlib.html#zlib_options) api docs). Set to `false` to disable. (`true`)
-`threshold` (`Number`): data is compressed only if the byte size is above this value (`1024`)
-`cookie` (`Object|Boolean`): configuration of the cookie that
contains the client sid to send as part of handshake response
headers. This cookie might be used for sticky-session. Defaults to not sending any cookie (`false`).
See [here](https://github.com/jshttp/cookie#options-1) for all supported options.
-`wsEngine` (`Function`): what WebSocket server implementation to use. Specified module must conform to the `ws` interface (see [ws module api docs](https://github.com/websockets/ws/blob/master/doc/ws.md)). Default value is `ws`. An alternative c++ addon is also available by installing `eiows` module.
-`cors` (`Object`): the options that will be forwarded to the cors module. See [there](https://github.com/expressjs/cors#configuration-options) for all available options. Defaults to no CORS allowed.
-`initialPacket` (`Object`): an optional packet which will be concatenated to the handshake packet emitted by Engine.IO.
-`allowEIO3` (`Boolean`): whether to support v3 Engine.IO clients (defaults to `false`)
-`close`
- Closes all clients
- **Returns** `Server` for chaining
-`handleRequest`
- Called internally when a `Engine` request is intercepted.
- **Parameters**
-`http.IncomingMessage`: a node request object
-`http.ServerResponse`: a node response object
- **Returns** `Server` for chaining
-`handleUpgrade`
- Called internally when a `Engine` ws upgrade is intercepted.
- **Parameters** (same as `upgrade` event)
-`http.IncomingMessage`: a node request object
-`net.Stream`: TCP socket for the request
-`Buffer`: legacy tail bytes
- **Returns** `Server` for chaining
-`attach`
- Attach this Server instance to an `http.Server`
- Captures `upgrade` requests for a `http.Server`. In other words, makes
a regular http.Server WebSocket-compatible.
- **Parameters**
-`http.Server`: server to attach to.
-`Object`: optional, options object
- **Options**
-`path` (`String`): name of the path to capture (`/engine.io`).
- Sends a message, performing `message = toString(arguments[0])` unless
sending binary data, which is sent as is.
- **Parameters**
-`String` |`Buffer` | `ArrayBuffer` | `ArrayBufferView`: a string or any object implementing `toString()`, with outgoing data, or a Buffer or ArrayBuffer with binary data. Also any ArrayBufferView can be sent as is.
-`Object`: optional, options object
-`Function`: optional, a callback executed when the message gets flushed out by the transport
- **Options**
-`compress` (`Boolean`): whether to compress sending data. This option might be ignored and forced to be `true` when using polling. (`true`)
- **Returns** `Socket` for chaining
-`close`
- Disconnects the client
- **Returns** `Socket` for chaining
### Client
<hr><br>
Exposed in the `eio` global namespace (in the browser), or by