Engine
is the implementation of transport-based cross-browser/cross-device
bi-directional communication layer for
Socket.IO.
var engine = require('engine.io')
, server = engine.listen(80)
server.on('connection', function (socket) {
socket.send('utf 8 string');
});
var engine = require('engine.io')
, http = require('http').createServer().listen(3000)
, server = engine.attach(http)
server.on('connection', function (socket) {
socket.on('message', function () { });
socket.on('close', function () { });
});
var engine = require('engine.io')
, server = new engine.Server()
server.on('connection', function (socket) {
socket.send('hi');
});
// …
httpServer.on('upgrade', function (req, socket, head) {
server.handleUpgrade(req, socket, head);
});
httpServer.on('request', function (req, res) {
server.handleRequest(req, res);
});
<script src="/path/to/engine.io.js"></script>
<script>
var socket = new eio.Socket('ws://localhost/');
socket.on('open', function () {
socket.on('message', function (data) { });
socket.on('close', function () { });
});
</script>
For more information on the client refer to the engine-client repository.
- Isomorphic with WebSocket.IO. You can switch between a WebSocket server
and a multi-transport server by changing the
require
. - 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)
- Written in readable vanilla JavaScript
These are exposed by require('engine.io')
:
flush
- Called when a socket buffer is being flushed.
- Arguments
Socket
: socket being flushedArray
: write buffer
drain
- Called when a socket buffer is drained
- Arguments
Socket
: socket being flushed
protocol
(Number): protocol revision numberServer
: Server class constructorSocket
: Socket class constructorTransport
(Function): transport constructortransports
(Object): map of available transports
listen
- Creates an
http.Server
which listens on the given port and attaches WS to it. It returns501 Not Implemented
for regular http requests. - Parameters
Number
: port to listen on.Function
: callback forlisten
.
- Returns
Server
- Creates an
attach
- Captures
upgrade
requests for ahttp.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
).policyFile
(Boolean
): whether to handle policy file requests (true
)destroyUpgrade
(Boolean
): destroy unhandled upgrade requests (true
)destroyUpgradeTimeout
(Number
): milliseconds after which unhandled requests are ended (1000
)- See Server options below for additional options you can pass
- Returns
Server
- Captures
The main server/manager. Inherits from EventEmitter.
connection
- Fired when a new connection is established.
- Arguments
Socket
: a Socket object
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.
- constructor
- Initializes the server
- Parameters
Object
: optional, options object
- Options
pingTimeout
(Number
): how many ms without a pong packet to consider the connection closed (60000
)pingInterval
(Number
): how many ms before sending a new ping packet (25000
)transports
(<Array> String
): transports to allow connections to (['polling', 'websocket', 'flashsocket']
)allowUpgrades
(Boolean
): whether to allow transport upgrades (true
)cookie
(String|Boolean
): name of the HTTP cookie that contains the client sid to send as part of handshake response headers. Set tofalse
to not send one. (io
)
close
- Closes all clients
- Returns
Server
for chaining
handleRequest
- Called internally when a
Engine
request is intercepted. - Parameters
http.ServerRequest
: a node request objecthttp.ServerResponse
: a node response object
- Returns
Server
for chaining
- Called internally when a
handleUpgrade
- Called internally when a
Engine
ws upgrade is intercepted. - Parameters (same as
upgrade
event)http.ServerRequest
: a node request objectnet.Stream
: TCP socket for the requestBuffer
: legacy tail bytes
- Returns
Server
for chaining
- Called internally when a
handleSocket
- Called with raw TCP sockets from http requests to intercept flash policy file requests
- Parameters
net.Stream
: TCP socket on which requests are listened
- Returns
Server
for chaining
A representation of a client. Inherits from EventEmitter.
close
- Fired when the client is disconnected.
- Arguments
String
: reason for closingObject
: description object (optional)
message
- Fired when the client sends a message.
- Arguments
String
: Unicode string
error
- Fired when an error occurs.
- Arguments
Error
: error object
flush
- Called when the write buffer is being flushed.
- Arguments
Array
: write buffer
drain
- Called when the write buffer is drained
packet
- Called when a socket received a packet (
message
,ping
) - Arguments
type
: packet typedata
: packet data (if type is message)
- Called when a socket received a packet (
packetCreate
- Called before a socket sends a packet (
message
,pong
) - Arguments
type
: packet typedata
: packet data (if type is message)
- Called before a socket sends a packet (
server
(Server): engine parent referencerequest
(http.ServerRequest): request that originated the Socketupgraded
(Boolean): whether the transport has been upgradedreadyState
(String): opening|open|closing|closedtransport
(Transport): transport reference
send
:- Sends a message, performing
message = toString(arguments[0])
. - Parameters
String
: a string or any object implementingtoString()
, with outgoing dataFunction
: optional, a callback executed when the message gets flushed out by the transport
- Returns
Socket
for chaining
- Sends a message, performing
close
- Disconnects the client
- Returns
Socket
for chaining
Exposed in the eio
global namespace (in the browser), or by
require('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.flashsocket
: WebSocket transport backed by flash.
- 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
$ make test
# make test-acceptance
And point browser/s to http://localhost:3000
.
$ make bench
$ make bench-server
And point browser/s to http://localhost:3000
.
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/FlashSocket based connections have two fundamental benefits:
- 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 theEngine
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.
-
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:
-
Proxies
Many corporate proxies block WebSocket traffic. -
Personal firewall and antivirus software
As a result of our research, we've found that at least 3 personal security applications block WebSocket traffic. -
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.
Socket.IO includes Engine
, therefore serving two clients is not necessary. If
you use Socket.IO, including
<script src="/socket.io/socket.io.js">
has you covered.
Absolutely. The SPEC
file contains the most up to date description of the implementation specification
at all times. If you're targeting the latest stable release of Engine
, make sure
to look at the file in the appropriate git branch/tag.
The Java/NIO implementation will be officially supported, and is being worked on by the author.
(The MIT License)
Copyright (c) 2011 Guillermo Rauch <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.