The realtime engine behind Socket.IO. Provides the foundation of a bidirectional connection between client and server
npm install engine.io

Engine.IO is the implementation of transport-based
cross-browser/cross-device bi-directional communication layer for
Socket.IO.
#### (A) Listening on a port
``js
const engine = require('engine.io');
const server = engine.listen(80);
server.on('connection', socket => {
socket.send('utf 8 string');
socket.send(Buffer.from([0, 1, 2, 3, 4, 5])); // binary data
});
`
#### (B) Intercepting requests for a http.Server
`js
const engine = require('engine.io');
const http = require('http').createServer().listen(3000);
const server = engine.attach(http);
server.on('connection', socket => {
socket.on('message', data => { });
socket.on('close', () => { });
});
`
#### (C) Passing in requests
`js
const engine = require('engine.io');
const server = new engine.Server();
server.on('connection', socket => {
socket.send('hi');
});
// …
httpServer.on('upgrade', (req, socket, head) => {
server.handleUpgrade(req, socket, head);
});
httpServer.on('request', (req, res) => {
server.handleRequest(req, res);
});
`
`html`
For more information on the client refer to the
engine-client repository.
- Maximum reliability. Connections are established even in the presence of:
- proxies and load balancers.
- personal firewall and antivirus software.
- for more information refer to Goals and Architecture sections
- Minimal client size aided by:
- lazy loading of flash transports.
- lack of redundant transports.
- Scalable
- load balancer friendly
- Future proof
- 100% Node.JS core style
- No API sugar (left for higher level projects)
#### Top-level
These are exposed by require('engine.io'):
##### Events
- flushSocket
- Called when a socket buffer is being flushed.
- Arguments
- : socket being flushedArray
- : write bufferdrain
- Socket
- Called when a socket buffer is drained
- Arguments
- : socket being flushed
##### Properties
- protocol _(Number)_: protocol revision numberServer
- : Server class constructorSocket
- : Socket class constructorTransport
- _(Function)_: transport constructortransports
- _(Object)_: map of available transports
##### Methods
- ()Server
- Returns a new instance. If the first argument is an http.Server then theServer
new instance will be attached to it. Otherwise, the arguments are passedServer
directly to the constructor.http.Server
- Parameters
- : optional, server to attach to.Object
- : optional, options object (see Server#constructor api docs below)
The following are identical ways to instantiate a server and then attach it.
`jshttp.createServer();
const httpServer; // previously created with from node.js api.
// create a server first, and then attach
const eioServer = require('engine.io').Server();
eioServer.attach(httpServer);
// or call the module as a function to get Server
const eioServer = require('engine.io')();
eioServer.attach(httpServer);
// immediately attach
const eioServer = require('engine.io')(httpServer);
// with custom options
const eioServer = require('engine.io')(httpServer, {
maxHttpBufferSize: 1e3
});
`
- listenhttp.Server
- Creates an which listens on the given port and attaches WS501 Not Implemented
to it. It returns for regular http requests.Number
- Parameters
- : port to listen on.Object
- : optional, options objectFunction
- : callback for listen.Server.attach
- Options
- All options from method, documented below.constructor
- Additionally See Server below for options you can pass for creating the new ServerServer
- Returns
`js
const engine = require('engine.io');
const server = engine.listen(3000, {
pingTimeout: 2000,
pingInterval: 10000
});
server.on('connection', / ... /);
`
- attachupgrade
- Captures requests for a http.Server. In other words, makeshttp.Server
a regular http.Server WebSocket-compatible.
- Parameters
- : server to attach to.Object
- : optional, options objectServer.attach
- Options
- All options from method, documented below.constructor
- Additionally See Server below for options you can pass for creating the new ServerServer
- Returns a new Server instance.
`js
const engine = require('engine.io');
const httpServer = require('http').createServer().listen(3000);
const server = engine.attach(httpServer, {
wsEngine: require('eiows').Server // requires having eiows as dependency
});
server.on('connection', / ... /);
`
#### Server
The main server/manager. _Inherits from EventEmitter_.
##### Events
- connectionSocket
- Fired when a new connection is established.
- Arguments
- : a Socket object
- initial_headersheaders
- Fired on the first request of the connection, before writing the response headers
- Arguments
- (Object): a hash of headersreq
- (http.IncomingMessage): the request
- headersheaders
- Fired on the all requests of the connection, before writing the response headers
- Arguments
- (Object): a hash of headersreq
- (http.IncomingMessage): the request
- connection_errorerror
- Fired when an error occurs when establishing the connection.
- Arguments
- : an object with following properties:req
- (http.IncomingMessage): the request that was droppedcode
- (Number): one of Server.errorsmessage
- (string): one of Server.errorMessagescontext
- (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 objectpingTimeout
- Options
- (Number): how many ms without a pong packet to20000
consider the connection closed ()pingInterval
- (Number): how many ms before sending a new ping25000
packet ()upgradeTimeout
- (Number): how many ms before an uncompleted transport upgrade is cancelled (10000)maxHttpBufferSize
- (Number): how many bytes or characters a message1E6
can be, before closing the session (to avoid DoS). Default
value is .allowRequest
- (Function): A function that receives a given handshakefn(err, success)
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: , wheresuccess
is a boolean value where false means that the request istransports
rejected, and err is an error code.
- (): transports to allow connections['polling', 'websocket']
to ()allowUpgrades
- (Boolean): whether to allow transport upgradestrue
()perMessageDeflate
- (Object|Boolean): parameters of the WebSocket permessage-deflate extensiontrue
(see ws module api docs). Set to 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 transportsfalse
(see zlib api docs). Set to 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 thatfalse
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 ().wsEngine
See here for all supported options.
- (Function): what WebSocket server implementation to use. Specified module must conform to the ws interface (see ws module api docs). 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 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
- Server
- Closes all clients
- Returns for chaininghandleRequest
- Engine
- Called internally when a request is intercepted.http.IncomingMessage
- Parameters
- : a node request objecthttp.ServerResponse
- : a node response objectServer
- Returns for chaininghandleUpgrade
- Engine
- Called internally when a ws upgrade is intercepted.upgrade
- Parameters (same as event)http.IncomingMessage
- : a node request objectnet.Stream
- : TCP socket for the requestBuffer
- : legacy tail bytesServer
- Returns for chainingattach
- http.Server
- Attach this Server instance to an upgrade
- Captures requests for a http.Server. In other words, makeshttp.Server
a regular http.Server WebSocket-compatible.
- Parameters
- : server to attach to.Object
- : optional, options objectpath
- Options
- (String): name of the path to capture (/engine.io).destroyUpgrade
- (Boolean): destroy unhandled upgrade requests (true)destroyUpgradeTimeout
- (Number): milliseconds after which unhandled requests are ended (1000)generateId
- http.IncomingMessage
- Generate a socket id.
- Overwrite this method to generate your custom socket id.
- Parameters
- : a node request object
- Returns A socket id for connected client.
#### Socket
A representation of a client. _Inherits from EventEmitter_.
##### Events
- closeString
- Fired when the client is disconnected.
- Arguments
- : reason for closingObject
- : description object (optional)message
- String
- Fired when the client sends a message.
- Arguments
- or Buffer: Unicode string or Buffer with binary contentserror
- Error
- Fired when an error occurs.
- Arguments
- : error objectupgrading
- Object
- Fired when the client starts the upgrade to a better transport like WebSocket.
- Arguments
- : the transportupgrade
- Object
- Fired when the client completes the upgrade to a better transport like WebSocket.
- Arguments
- : the transportflush
- Array
- Called when the write buffer is being flushed.
- Arguments
- : write bufferdrain
- packet
- Called when the write buffer is drained
- message
- Called when a socket received a packet (, ping)type
- Arguments
- : packet typedata
- : packet data (if type is message)packetCreate
- message
- Called before a socket sends a packet (, ping)type
- Arguments
- : packet typedata
- : packet data (if type is message)heartbeat
- ping
- Called when or pong packed is received (depends of client version)
##### Properties
- id _(String)_: unique identifierserver
- _(Server)_: engine parent referencerequest
- _(http.IncomingMessage)_: request that originated the Socketupgraded
- _(Boolean)_: whether the transport has been upgradedreadyState
- _(String)_: opening|open|closing|closedtransport
- _(Transport)_: transport reference
##### Methods
- send:message = toString(arguments[0])
- Sends a message, performing unlessString
sending binary data, which is sent as is.
- Parameters
- | 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 objectFunction
- : optional, a callback executed when the message gets flushed out by the transportcompress
- Options
- (Boolean): whether to compress sending data. This option might be ignored and forced to be true when using polling. (true)Socket
- Returns for chainingclose
- Socket
- Disconnects the client
- Returns for chaining
Exposed in the eio global namespace (in the browser), or byrequire('engine.io-client') (in Node.JS).
For the client API refer to the
engine-client repository.
Engine.IO is powered by debug.
In order to see all the debug output, run your app with the environment variable
DEBUG including the desired scope.
To see the output from all of Engine.IO's debugging scopes you can use:
``
DEBUG=engine* node myapp
- polling: XHR / JSONP polling transport.websocket
- : WebSocket transport.
- engine.io-conflation: Makes conflation and aggregation of messages straightforward.
The support channels for engine.io are the same as socket.io:
- irc.freenode.net #socket.io
- Google Groups
- Website
To contribute patches, run tests or benchmarks, make sure to clone the
repository:
``
git clone git://github.com/LearnBoost/engine.io.git
Then:
``
cd engine.io
npm install
Tests run with npm test. It runs the server tests that are aided byengine.io-client
the usage of .
Make sure npm install is run first.
The main goal of Engine is ensuring the most reliable realtime communication.
Unlike the previous Socket.IO core, it always establishes a long-polling
connection first, then tries to upgrade to better transports that are "tested" on
the side.
During the lifetime of the Socket.IO projects, we've found countless drawbacks
to relying on HTML5 WebSocket or Flash Socket as the first connection
mechanisms.
Both are clearly the _right way_ of establishing a bidirectional communication,
with HTML5 WebSocket being the way of the future. However, to answer most business
needs, alternative traditional HTTP 1.1 mechanisms are just as good as delivering
the same solution.
WebSocket based connections have two fundamental benefits:
1. Better server performance
- _A: Load balancers_
Load balancing a long polling connection poses a serious architectural nightmare
since requests can come from any number of open sockets by the user agent, but
they all need to be routed to the process and computer that owns the Engine
connection. This negatively impacts RAM and CPU usage.
- _B: Network traffic_
WebSocket is designed around the premise that each message frame has to be
surrounded by the least amount of data. In HTTP 1.1 transports, each message
frame is surrounded by HTTP headers and chunked encoding frames. If you try to
send the message _"Hello world"_ with xhr-polling, the message ultimately
becomes larger than if you were to send it with WebSocket.
- _C: Lightweight parser_
As an effect of B, the server has to do a lot more work to parse the network
data and figure out the message when traditional HTTP requests are used
(as in long polling). This means that another advantage of WebSocket is
less server CPU usage.
2. Better user experience
Due to the reasons stated in point 1, the most important effect of being able
to establish a WebSocket connection is raw data transfer speed, which translates
in _some_ cases in better user experience.
Applications with heavy realtime interaction (such as games) will benefit greatly,
whereas applications like realtime chat (Gmail/Facebook), newsfeeds (Facebook) or
timelines (Twitter) will have negligible user experience improvements.
Having said this, attempting to establish a WebSocket connection directly so far has
proven problematic:
1. Proxies
Many corporate proxies block WebSocket traffic.
2. Personal firewall and antivirus software
As a result of our research, we've found that at least 3 personal security
applications block WebSocket traffic.
3. Cloud application platforms
Platforms like Heroku or No.de have had trouble keeping up with the fast-paced
nature of the evolution of the WebSocket protocol. Applications therefore end up
inevitably using long polling, but the seamless installation experience of
Socket.IO we strive for (_"require() it and it just works"_) disappears.
Some of these problems have solutions. In the case of proxies and personal programs,
however, the solutions many times involve upgrading software. Experience has shown
that relying on client software upgrades to deliver a business solution is
fruitless: the very existence of this project has to do with a fragmented panorama
of user agent distribution, with clients connecting with latest versions of the most
modern user agents (Chrome, Firefox and Safari), but others with versions as low as
IE 5.5.
From the user perspective, an unsuccessful WebSocket connection can translate in
up to at least 10 seconds of waiting for the realtime application to begin
exchanging data. This perceptively hurts user experience.
To summarize, Engine focuses on reliability and user experience first, marginal
potential UX improvements and increased server performance second. Engine is the
result of all the lessons learned with WebSocket in the wild.
The main premise of Engine, and the core of its existence, is the ability to
swap transports on the fly. A connection starts as xhr-polling, but it can
switch to WebSocket.
The central problem this poses is: how do we switch transports without losing
messages?
Engine only switches from polling to another transport in between polling
cycles. Since the server closes the connection after a certain timeout when
there's no activity, and the polling transport implementation buffers messages
in between connections, this ensures no message loss and optimal performance.
Another benefit of this design is that we workaround almost all the limitations
of Flash Socket, such as slow connection times, increased file size (we can
safely lazy load it without hurting user experience), etc.
Absolutely. Although the recommended framework for building realtime applications
is Socket.IO, since it provides fundamental features for real-world applications
such as multiplexing, reconnection support, etc.
Engine is to Socket.IO what Connect is to Express. An essential piece for building
realtime frameworks, but something you _probably_ won't be using for building
actual applications.
No. The main reason is that Engine is meant to be bundled with frameworks.Engine
Socket.IO includes , therefore serving two clients is not necessary. If
you use Socket.IO, including
`html