/ never-odd-or-even / back / node_modules / engine.io /

[ICO]NameLast modifiedSizeDescription
[PARENTDIR]Parent Directory  -  
[DIR]build/2 years ago -  
[DIR]node_modules/2 years ago -  
[   ]LICENSE39 years ago1.1K 
[TXT]README.md39 years ago 22K 
[   ]package.json2 years ago3.2K 
[   ]wrapper.mjs39 years ago129  
README.md

Engine.IO: the realtime engine

Build Status NPM version

Engine.IO is the implementation of transport-based cross-browser/cross-device bi-directional communication layer for Socket.IO.

How to use

Server

(A) Listening on a port

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

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

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);
});

Client

<script src="/path/to/engine.io.js"></script>
<script>
  const socket = new eio.Socket('ws://localhost/');
  socket.on('open', () => {
    socket.on('message', data => {});
    socket.on('close', () => {});
  });
</script>

For more information on the client refer to the engine-client repository.

What features does it have?

API

Server



Top-level

These are exposed by require('engine.io'):

Events
Properties
Methods
const httpServer; // previously created with `http.createServer();` 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
});
const engine = require('engine.io');
const server = engine.listen(3000, {
  pingTimeout: 2000,
  pingInterval: 10000
});

server.on('connection', /* ... */);
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
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.

Methods


Socket

A representation of a client. Inherits from EventEmitter.

Events
Properties
Methods

Client



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.

Debug / logging

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

Transports

Plugins

Support

The support channels for engine.io are the same as socket.io:

Development

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

Tests run with npm test. It runs the server tests that are aided by the usage of engine.io-client.

Make sure npm install is run first.

Goals

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.

Architecture

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.

FAQ

Can I use engine without Socket.IO ?

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.

Does the server serve the client?

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.

Can I implement Engine in other languages?

Absolutely. The engine.io-protocol repository contains the most up-to-date description of the specification at all times.

License

(The MIT License)

Copyright (c) 2014 Guillermo Rauch &lt;guillermo@learnboost.com&gt;

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.

Apache/2.4.38 (Debian) Server at www.karls.computer Port 80