Primus

Primus, the creator god of the transformers & an abstraction layer for real...

README

Primus

Version npmBuild StatusCoverage StatusIRC channel

Primus, the creator god of transformers but now also known as universal wrapper
for real-time frameworks. There are a lot of real-time frameworks available for
Node.js and they all have different opinions on how real-time should be done.
Primus provides a common low level interface to communicate in real-time using
various real-time frameworks.

Advantages


1. Effortless switching between real-time frameworks by changing one single line
   of code. No more API rewrites needed when your project requirements change,
   the framework gets abandoned or simply breaks down.
2. Built-in reconnect, it just works. The reconnect is controlled by a
   randomised exponential back-off algorithm to reduce server stress.
3. Offline detection, Primus is smart enough to detect when users drop their
   internet connection (switching WIFI points/cell towers for example) and
   reconnects when they are back online.
4. Automatically encodes and decodes messages using custom parsers. Can be
   easily switched for binary encoding for example.
5. A clean, stream-compatible interface for the client and server. You can
   just stream#pipe data around. In addition to that, the client works on
   Node.js as well, write once, run it everywhere.
6. Fixes various of bugs in the supported frameworks and additional stability
   patches to improve real-time communication.
8. Comes with an amazing plugin interface to keep the core library as fast and
   lean as possible while still allowing the server and the client to be
   extended.
9. Last but not least, Primus is built with love, passion and dedication to the
   real-time web.

  1. ```
  2. If you have questions or need help with primus, come chat in our IRC room:

  3.    server: irc.freenode.net
  4.    room: #primus
  5. ```

Installation


Primus is released on npm and can be installed using:

  1. ```
  2. npm install primus --save
  3. ```

Before Starting


If you deploy your application behind a reverse proxy (Nginx, HAProxy, etc.) you
might need to add WebSocket specific settings to its configuration files. If
you intend to use WebSockets, please ensure that these settings have been added.
There are some example configuration files available in the
repository.

Table of Contents


  - Engine.IO
  - Faye
  - SockJS
  - uws
- FAQ
  - Scaling
  - Cluster
  - Express
  - RequireJS
  - History
  - Protocol

Getting started


Primus doesn't ship with real-time frameworks as dependencies, it assumes that
you as user add them yourself as a dependency. This is done to keep the module
as lightweight as possible. This works because require in will walk through
your directories searching for node_module folders that have these matching
dependencies.

Primus needs to be "attached" to a HTTP compatible server. These includes the
built-in http and https servers but also the spdy module as it has the
same API as node servers. Creating a new Primus instance is relatively
straightforward:

  1. ``` js
  2. 'use strict';

  3. var Primus = require('primus')
  4.   , http = require('http');

  5. var server = http.createServer(/* request handler */)
  6.   , primus = new Primus(server, {/* options */});
  7. ```
The following options can be provided:

Name                | Description                               | Default
authorization       | Authorization handler                     | null
pathname            | The URL namespace that Primus can own     | /primus
parser              | Message encoder for all communication     | JSON
transformer         | The transformer we should use internally  | websockets
plugin              | The plugins that should be applied        | {}
pingInterval        | Interval at which heartbeats are sent     | 30000
global              | Set a custom client class / global name   | Primus
compression         | Use permessage-deflate / HTTP compression | false
maxLength           | Maximum allowed packet size, in bytes     | 10485760
transport           | Transformer specific configuration        | {}
idGenerator         | Custom spark id generator function        | undefined
origins             | cors List of origins                  | *
methods             | cors List of accepted HTTP methods    | GET,HEAD,PUT,POST,DELETE,OPTIONS
credentials         | cors Allow sending of credentials     | true
maxAge              | cors Cache duration of CORS preflight | 30 days
headers             | cors Allowed headers                  | false
exposed             | cors Headers exposed to the client    | false

The options that are prefixed with cors are supplied to our
access-control module which handles
HTTP Access Control (CORS), so for a more detailed explanation of these options
check it out.

The transport option allows you to use any configuration option supported by
the underlying real-time framework. Its use is discouraged as these options
are framework specific and no longer work if you change transformer. Our advise
is to use it only if you know what you are doing and if you need fine-grained
control over the real-time framework. Please also keep in mind that some of
these options are overriden by Primus.

The pingInterval option specifies the interval at which heartbeats are
transmitted. It is possible to completely disable the heartbeats by setting the
value of the pingInterval option to false.

The idGenerator option can be used to define a function which will be called
to set each [spark.id](#sparkid). The generator function should return
a unique string each time it is invoked. If idGenerator is not defined, Primus
will try to use ids provided by the transformer. If the transformer does not
provide ids, Primus will use nanoid to generate
Spark ids.

If you don't have a pre-existing server where you want or can attach your Primus
server to you can also use the Primus.createServer convenience method. The
createServer method will automatically:

- Setup a HTTP, HTTPS or SPDY server for you on the given port number.
- Setup your Primus server with the given configuration.
- Listen on the HTTP, HTTPS, SPDY server.
- Attach a primus.on('connection') listener.
- Return the created Primus instance.

  1. ``` js
  2. Primus.createServer(function connection(spark) {

  3. }, { port: 8080, transformer: 'websockets' });
  4. ```

In the above example we automatically create a HTTP server which will listen
on port 8080, a primus instance with the websockets transformer and start
listening for incoming connections. The supplied function in the
Primus.createServer method is optional. You can just listen for incoming
connections your self using the returned Primus instance. If you want to listen to
a HTTPS or SPDY server, which is recommended, you can directly pass the SPDY and
HTTPS certs/keys/pfx files in the options object:

  1. ``` js
  2. var primus = Primus.createServer({
  3.   port: 443,
  4.   root: '/folder/with/https/cert/files',
  5.   cert: 'myfilename.cert',
  6.   key: 'myfilename.cert',
  7.   ca: 'myfilename.ca',
  8.   pfx: 'filename.pfx',
  9.   passphrase: 'my super sweet password'
  10. });

  11. primus.on('connection', function (spark) {
  12.   spark.write('hello connnection');
  13. });
  14. ```

Primus.createServer returns a warning when it starts a HTTP server. The
warning advises you to use a HTTPS server and can be disabled setting the
option iknowhttpsisbetter to true.

Client library


As most libraries come with their own client-side framework for making the
connection we've also created a small wrapper for this. The library can be
retrieved using:

  1. ``` js
  2. primus.library();
  3. ```

Which returns the client-side library as a string (which can then be minified or
even have more code added to it). It does not come pre-minified as that is out
of the scope of this project. You can store this on a CDN or on your static server.
Do whatever you want with it, but remember to regenerate it every time you change
Primus server options. This is important because some properties of the client
are set using the server configuration. For example if you change the
pathname, the client should be regenerated to reflect that change and work
correctly. We advise you to regenerate the library every time you redeploy so
you always have a client compatible with your back-end. To save the file you
can use:

  1. ``` js
  2. primus.save(__dirname +'/primus.js');
  3. ```

This will store the compiled library in your current directory. If you want to
save it asynchronously, you can supply the method with a callback method:

  1. ``` js
  2. primus.save(__dirname +'/primus.js', function save(err) {

  3. });
  4. ```

But to make it easier for you during development we've automatically added an
extra route to the supplied HTTP server, this will serve the library for you so
you don't have to save it. Please note, that this route isn't optimised for
serving static assets and should only be used during development. In your HTML
page add:

  1. ``` html
  2. <script src="/primus/primus.js"></script>
  3. ```

As you can see, it will use the /primus pathname by default. Primus needs to
own the whole path/namespace in order to function properly as it will forward
all other requests directly in to the transformers so they can work their magic.
If you already have a static folder with the name primus you can change the
pathname to something different and still make this work. But you would of
course need to update the src attribute of the script tag to set the correct
location. It's always available at:

  1. ```
  2. <protocol>:////primus.js
  3. ```

Here `` is the `pathname` set in server options above. The client
is cross domain compatible so you don't have to serve it from the
same domain you're running Primus on. But please note, that the real-time
framework you're using might be tied to same domain restrictions.

Once you're all set up you can start listening for connections. These
connections are announced through the connection event.

  1. ``` js
  2. primus.on('connection', function (spark) {
  3.   // spark is the new connection.
  4. });
  5. ```

Disconnects are announced using a disconnection event:

  1. ``` js
  2. primus.on('disconnection', function (spark) {
  3.   // the spark that disconnected
  4. });
  5. ```

The spark argument is the actual real-time socket/connection. Sparks have a
really low level interface and only expose a couple properties that are cross
engine supported. The interface is modeled towards a Node.js stream compatible
interface. So this will include all methods that are available on the [stream
interface](https://nodejs.org/api/stream.html) including Spark#pipe.

spark.headers


The spark.headers property contains the headers of either the request
that started a handshake with the server or the headers of the actual real-time
connection. This depends on the module you are using.

*Please note that sending custom headers from the client to the server is
impossible as not all transports that these transformers support can add custom
headers to a request (JSONP for example). If you need to send custom data, use a
query string when connecting*

spark.address


The spark.address property contains the ip and port of the
connection. If you're running your server behind a reverse proxy it will
automatically use the x-forwarded-for header. This way you will always have
the address of the connecting client and not the IP address of your proxy.

*Please note that the port is probably out of date by the time you're going
to read it as it's retrieved from an old request, not the request that is
active at the time you access this property.*

spark.query


The spark.query contains the query string you used to connect to the server. It's
parsed as an object. Please note that this may not be available for all supported
transformers.

spark.socket


The spark.socket is set to the underlying socket of the transformer. This is not
necessarily a raw Socket and will differ from transformer to transformer.

spark.id


This is a unique id that we use to identify this single connection with. Normally
the frameworks refer to this as a sessionid, which is confusing as it's only
used for the duration of one single connection. You should not see this as a
"session id", and rather expect it to change between disconnects and reconnects.

spark.request


The spark.request gives you access to the HTTP request that was used to
initiate the real-time connection with the server. Please note that this request
is already answered and closed (in most cases) so do not attempt to write or
answer it anyway. But it might be useful to access methods that get added by
middleware layers, etc.

spark.write(data)


You can use the spark.write method to send data over the socket. The data is
automatically encoded for you using the parser that you've set while creating
the Primus server instance. This method always returns true on success and
false on failure so back pressure isn't handled.

  1. ``` js
  2. spark.write({ foo: 'bar' });
  3. ```

spark.end(data, options)


You can use spark.end to close the connection. This method takes two optional
arguments. The first, if provided, is the data to send to the client before
closing the connection. The second is an options object used to customize the
behavior of the method. By default the spark.end method closes the connection
in a such way that the client knows it was intentional and it doesn't attempt a
reconnection.

  1. ``` js
  2. spark.end(); // the client doesn't reconnect automatically
  3. ```

You can change this behavior and trigger a client-side reconnection using the
reconnect option.

  1. ``` js
  2. spark.end(undefined, { reconnect: true }); // trigger a client-side reconnection
  3. ```

spark.emits(event, parser)


This method is mostly used internally. It works similarly to the native bind
function, returning a function that emits the assigned event every time it's
called. If the last argument is a function, it will be used to parse the
arguments of the returned function. The parser is optional and always async,
its first argument is a callback that follows the usual error first pattern,
all successive arguments are the ones to parse. Using the parser you can
reduce the arguments down to a single value, remove them completely or prevent
the event from being emitted. See emits for
detailed usage instructions.

  1. ``` js
  2. spark.emits('event', function parser(next, structure) {
  3.   next(undefined, structure.data);
  4. });
  5. ```

Please note that the data that is received here isn't decoded yet.

spark.on('data')


The data event is emitted when a message is received from the client. It's
automatically decoded by the specified decoder.

  1. ``` js
  2. spark.on('data', function message(data) {
  3.   // the message we've received.
  4. });
  5. ```

spark.on('end')


The end event is emitted when the client has disconnected.

  1. ``` js
  2. primus.on('connection', function (spark) {
  3.   console.log('connection has the following headers', spark.headers);
  4.   console.log('connection was made from', spark.address);
  5.   console.log('connection id', spark.id);

  6.   spark.on('data', function (data) {
  7.     console.log('received data from the client', data);

  8.     //
  9.     // Always close the connection if we didn't receive our secret imaginary
  10.     // handshake.
  11.     //
  12.     if ('foo' !== data.secrethandshake) spark.end();
  13.     spark.write({ foo: 'bar' });
  14.     spark.write('banana');
  15.   });

  16.   spark.write('Hello world');
  17. })
  18. ```

Connecting from the Browser


Primus comes with its client framework which can be compiled using
primus.library() as mentioned above. To create a connection you can simply
create a new Primus instance:

  1. ``` js
  2. var primus = new Primus(url, { options });

  3. //
  4. // But it can be easier, with some syntax sugar.
  5. //
  6. var primus = Primus.connect(url, { options });
  7. ```

The URL should confirm the following conditions:

- It should include the protocol it needs to connect with. This can either be
  http or https. We recommend that you're using HTTPS for all your
  connections as this prevents connection blocking by firewalls and anti-virus
  programs.
- The URL should not include a pathname. The pathname is configured by the
  server (See: getting-started) and needs to be configured
  there as it will be compiled in to the primus.js client file.

If no url argument is passed, it will default to the current URL.

The following options can be provided:

Name                | Description                             | Default
[reconnect]         | Configures the exponential back off     | {}
timeout             | Connect time out                        | 10000 ms
pingTimeout         | Max time to wait for a server ping      | 45000 ms
[strategy]          | Our reconnect strategies                | "disconnect,online,timeout"
manual              | Manually open the connection            | false
websockets          | Should we use WebSockets                | Boolean, is detected
network             | Use native online/offline detection | Boolean, is feature detected
transport           | Transport specific configuration        | {}
queueSize           | Number of messages that can be queued   | Infinity

There are 2 important options that we're going to look a bit closer at.

Reconnect

When the connection goes down unexpectedly an automatic reconnect process is
started. It uses a randomised exponential back-off algorithm to prevent clients
from DDoSing your server when you reboot as they will all be re-connecting at
different times. The reconnection can be configured using the options argument
in Primus and you should add these options to the reconnect property:

Name                | Description                              | Default
max                 | Maximum delay for a reconnection attempt | Infinity
min                 | Minimum delay for a reconnection attempt | 500 ms
retries             | Maximum amount of attempts               | 10
reconnect timeout   | Maximum time for an attempt to complete  | 30000 ms
factor              | Exponential back off factor              | 2

  1. ``` js
  2. primus = Primus.connect(url, {
  3.   reconnect: {
  4.       max: Infinity // Number: The max delay before we try to reconnect.
  5.     , min: 500 // Number: The minimum delay before we try reconnect.
  6.     , retries: 10 // Number: How many times we should try to reconnect.
  7.   }
  8. });
  9. ```

When you're going to customize min please note that it will grow
exponentially e.g. 500 -> 1000 -> 2000 -> 4000 -> 8000 and is randomized
so expect to have slightly higher or lower values.

Please note that when we reconnect, we will receive a new connection event on
the server and a new open event on the client, as the previous connection was
completely dead and should therefore be considered a new connection.

If you are interested in learning more about the backoff algorithm you might
want to read http://dthain.blogspot.nl/2009/02/exponential-backoff-in-distributed.html

Strategy

The strategy allows you to configure when you want a reconnect operation to
kick in. We're providing some sane defaults for this but we still want to
provide users with highest level of customization:

disconnect
    Reconnect when we detect an unintentional disconnect in the connection.
online
    Reconnect when the browser went from an offline event to an online event.
timeout
    Reconnect when we failed to establish our initial connection. This can
    happen because we took too long to connect or because there was an error
    while we tried to connect (which happens when you connect to a dead server)

You can supply these options as a comma-separated String:

  1. ``` js
  2. var primus = new Primus(url, { strategy: 'online, timeout, disconnect' })
  3. ```

Or as an Array:

  1. ``` js
  2. var primus = new Primus(url, { strategy: [ 'online', 'timeout', 'disconnect' ]});
  3. ```

We'll try to normalize everything as much as possible, we toLowerCase everything
and join it back to a readable string so if you wrote dIsconNect it will get
normalized to disconnect.

If you are using authentication you should disable the timeout strategy as
there is no way of detecting the difference between a failed authorization and a
failed connect. If you leave this enabled with authorization every unauthorized
access will try to reconnect again.

We automatically disable this for you when you've set the authorization before
you save the library.

But there are always use cases where reconnection is not advised for your
application. In these cases we've provided a way to completely disable the
reconnection, this is done by setting the strategy to false:

  1. ``` js
  2. var primus = new Primus(url, { strategy: false });
  3. ```
If you want to manually control the reconnection you can call primus.end()
to close the connection and primus.open() to establish a new one. Be sure
to use primus.open() correctly, see below for details.

[reconnect]: #reconnect
[strategy]: #strategy

transport

The transport object allows you to add a transport specific configuration.
We only recommend using this if you understand and accept the following
consequences:

- Primus will try to override configuration properties that are needed to
  ensure a correct functioning.
- We might start using options without any announcement or major version bump.
- Expect your client and its connection to malfunction once you switch between
  different transports, as these configurations are specific to the bundled
  transformer library/client.
- Bugs and bug reports caused by using this functionality are closed
  immediately.

Having that said, this gives you total freedom while still getting the benefits
of Primus.

primus.open()


This method opens a connection with the server. By default it is called
automatically when the Primus instance is created, but there are cases where
it's desirable to open the connection manually. To do this set the manual
option to true and when you have the Primus instance call the method:

  1. ``` js
  2. primus.open();
  3. ```

When you call primus.open() you should make sure that the connection is
totally dead (e.g. after an end event) and primus isn't already trying or
planning to reconnect.

primus.write(message)


Once you've created your Primus instance you're ready to go. When you want to
write data to your server you can just call the .write method:

  1. ``` js
  2. primus.write('message');
  3. ```

It automatically encodes your messages using the parser that you've specified on
the server. So sending objects back and forth between the server is nothing
different then just writing:

  1. ``` js
  2. primus.write({ foo: 'bar' });
  3. ```

When you are sending messages to the server, you don't have to wait for the
open event to happen, the client will automatically buffer all the data you've
send and automatically write it to the server once it's connected. The client
supports a couple of different events.

primus.on('data')


The data event is the most important event of the whole library. It's emitted
when we receive data from the server. The data that is received is already
decoded by the specified parser.

  1. ``` js
  2. primus.on('data', function message(data) {
  3.   console.log('Received a new message from the server', data);
  4. });
  5. ```

primus.on('open')


The open event is emitted when we've successfully created a connection with
the server. It will also be emitted when we've successfully reconnected after the
connection goes down unintentionally.

  1. ``` js
  2. primus.on('open', function open() {
  3.   console.log('Connection is alive and kicking');
  4. });
  5. ```

primus.on('error')


The error event is emitted when something breaks that is out of our control.
Unlike Node.js, we do not throw an error if no error event listener is
specified. In general, when there is an active connection, it is not directly
closed when an error event is emitted. The cause of an error, in fact, could
be that the parser failed to encode or decode a message. In this case we only
emit the error, discard the message and keep the connection alive. An error
event can also be emitted when a connection fails to establish. When this
happens the client automatically tries to reconnect, unless the connection gets
closed for some other reason. The only exception is when there is an
authorization hook. If we get an error when connecting to a server where
authorization is required, we simply close the connection, as we can't
determinate if the error is the result of an unauthorized access or not.

  1. ``` js
  2. primus.on('error', function error(err) {
  3.   console.error('Something horrible has happened', err.stack);
  4. });
  5. ```

primus.on('reconnect')


The reconnect event is emitted when we're attempting to reconnect to the
server. This all happens transparently and it's just a way for you to know when
these reconnects are actually happening.

  1. ``` js
  2. primus.on('reconnect', function (opts) {
  3.   console.log('Reconnection attempt started');
  4. });
  5. ```

primus.on('reconnect scheduled')


Looks a lot like the reconnect event mentioned above, but it's emitted when
we've detected that connection went/is down and we're going to start a reconnect
operation. This event would be ideal to update your application's UI when the
connection is down and you are trying to reconnect in x seconds.

  1. ``` js
  2. primus.on('reconnect scheduled', function (opts) {
  3.   console.log('Reconnecting in %d ms', opts.scheduled);
  4.   console.log('This is attempt %d out of %d', opts.attempt, opts.retries);
  5. });
  6. ```

primus.on('reconnected')


The client successfully reconnected with the server.

  1. ``` js
  2. primus.on('reconnected', function (opts) {
  3.   console.log('It took %d ms to reconnect', opts.duration);
  4. });
  5. ```

primus.on('reconnect timeout')


The reconnect timeout event is emitted when a reconnection attempt takes too
much time. This can happen for example when the server does not answer a request
in a timely manner.

  1. ``` js
  2. primus.on('reconnect timeout', function (err, opts) {
  3.   console.log('Timeout expired: %s', err.message);
  4. });
  5. ```

After this event a whole new reconnection procedure is automatically started, so
you don't have to worry about it.

primus.on('reconnect failed')


This event is emitted when the reconnection failed, for example when all
attempts to reconnect have been unsuccessful.

  1. ``` js
  2. primus.on('reconnect failed', function (err, opts) {
  3.   console.log('The reconnection failed: %s', err.message);
  4. });
  5. ```

primus.on('end')


The end event is emitted when we've closed the connection. When this event is
emitted you should consider your connection to be fully dead with no way of
reconnecting. But it's also emitted when the server closes the connection.

  1. ``` js
  2. primus.on('end', function () {
  3.   console.log('Connection closed');
  4. });
  5. ```

primus.end()


When you want to close the connection you can call the primus.end() method.
After this the connection should be considered dead and a new connection needs
to be made using Primus.connect(url) or primus = new Primus(url) if you want
to talk with the server again.

  1. ``` js
  2. primus.end();
  3. ```

primus.destroy()


This method literally destroys the primus instance. Internally it calls the
primus.end() method but it also frees some potentially heavy objects like
the underlying socket, the timers, the message transformers, etc. It also
removes all the event listeners but before doing that it emits a final destroy
event. Keep in mind that once this method is executed, you can no longer use
primus.open() on the same primus instance.

  1. ``` js
  2. primus.on('destroy', function () {
  3.   console.log('Feel the power of my lasers!');
  4. });

  5. primus.destroy();
  6. ```

primus.emits(event, parser)


This method is analogous to the [spark.emits](#sparkemitsevent-parser) method.
It returns a function that emits the given event every time it's called. See
emits for detailed usage instructions.

  1. ``` js
  2. primus.emits('event', function parser(next, structure) {
  3.   next(undefined, structure.data);
  4. });
  5. ```

primus.id(callback)


There are cases where it is necessary to retrieve the [spark.id](#sparkid)
from the client. To make this easier, we added a primus.id() method that
takes a callback function to which the id will be passed.

  1. ``` js
  2. primus.id(function (id) {
  3.   console.log(id);
  4. });
  5. ```

Connecting from the server


There are two ways of creating a server side client.

1. When you've created your primus instance you can access the Socket
   property on it. This Socket is automatically configured to connect to the
   correct pathname, using the same transformer and parser that you've
   specified when you created your primus instance.

  1. ``` js
  2.    var primus = new Primus(server, { transformer: transformer, parser: parser })
  3.      , Socket = primus.Socket;

  4.    var client = new Socket('http://localhost:8080');
  5.    //
  6.    // It has the same interface as the client, so you can just socket.write or
  7.    // listen for the `open` events etc.
  8.    //
  9. ```
2. You might need to connect from a different node process where you don't have
   access to your primus instance and the compatible Socket instance. For
   these cases there a special createSocket method where you can specify the
   transformer, parser, plugin that you are using on your server to create
   another compatible socket.

  1. ``` js
  2.    var Primus = require('primus') // Primus library from npm install primus
  3.      , Socket = Primus.createSocket({ transformer: transformer, parser: parser })
  4.      , client = new Socket('http://localhost:8080');
  5. ```

When you are using plugins with Primus make sure you add them before you
reference the primus.Socket or it will compile a client without your plugins.
If you're using the Primus.createSocket api you can directly supply the
plugins as part of the options as it supports plugin object:

  1. ``` js
  2. var Socket = Primus.createSocket({
  3.   transformer: transformer,
  4.   parser: parser,
  5.   plugin: {
  6.     'my-emitter': require('my-emitter'),
  7.     'substream': require('substream')
  8.   }
  9. });
  10. ```

The constructor returned by primus.Socket or Primus.createSocket has the
same signature of the constructor used to connect from the browser. This
means that you can use all the options mentioned in the previous

  1. ``` js
  2. var Socket = Primus.createSocket()
  3.   , client = new Socket('http://localhost:8080', { options });
  4. ```

If you do not know which transformer and parser are used on the server, we
expose a small JSON "spec" file that exposes this information. The specification
can be reached on the `//spec` and will output the following JSON
document:

  1. ``` json
  2.   {
  3.     "version":"2.4.0",
  4.     "pathname":"/primus",
  5.     "parser":"json",
  6.     "transformer":"websockets"
  7.   }
  8. ```

Authorization


Server


Primus has a built-in auth hook that allows you to leverage the basic auth
header to validate the connection. To setup the optional auth hook, use the
Primus#authorize method:

  1. ``` js
  2. var authParser = require('basic-auth-parser');

  3. //
  4. // Add hook on server
  5. //
  6. primus.authorize(function (req, done) {
  7.   var auth;

  8.   try { auth = authParser(req.headers['authorization']) }
  9.   catch (ex) { return done(ex) }

  10.   //
  11.   // Do some async auth check
  12.   //
  13.   authCheck(auth, done);
  14. });

  15. primus.on('connection', function (spark) {
  16.   //
  17.   // You only get here if you make it through the auth hook!
  18.   //
  19. });
  20. ```

In this particular case, if an error is passed to done by authCheck or
the exception handler then the connection attempt will never make it to the
primus.on('connection') handler.

The error you pass can either be a string or an object. If an object, it can
have the following properties which affect the response sent to the client:

- statusCode: The HTTP status code returned to the client. Defaults to 401.
- authenticate: If set and statusCode is 401 then a WWW-Authenticate
  header is added to the response, with a value equal to the authenticate
  property's value.
- message: The error message returned to the client. The response body will be
  {error: message}, JSON-encoded.

If the error you pass is a string then a 401 response is sent to the client
with no WWW-Authenticate header and the string as the error message.

For example to send 500 when an exception is caught, 403 for forbidden users
and details of the basic auth scheme being used when authentication fails:

  1. ``` js
  2. primus.authorize(function (req, done) {
  3.   var auth;

  4.   if (req.headers.authorization) {
  5.     try { auth = authParser(req.headers.authorization) }
  6.     catch (ex) {
  7.       ex.statusCode = 500;
  8.       return done(ex);
  9.     }

  10.     if ((auth.scheme === 'myscheme') &&
  11.         checkCredentials(auth.username, auth.password)) {
  12.       if (userAllowed(auth.username)) {
  13.         return done();
  14.       } else {
  15.         return done({ statusCode: 403, message: 'Go away!' });
  16.       }
  17.     }
  18.   }

  19.   done({
  20.     message: 'Authentication required',
  21.     authenticate: 'Basic realm="myscheme"'
  22.   });
  23. });
  24. ```

Please note that the auth hook is run each and every time a request is made to
the server.

Client


Unfortunately, the amount of detail you get in your client when authorization
fails depends on the transformer in use. Most real-time frameworks supported
by Primus don't expose the status code, headers or response body.

The WebSocket transformer's underlying transport socket will fire an
unexpected-response event with the HTTP request and response:

  1. ``` js
  2. primus.on('outgoing::open', function () {
  3.   primus.socket.on('unexpected-response', function (req, res) {
  4.     console.error(res.statusCode);
  5.     console.error(res.headers['www-authenticate']);

  6.     //
  7.     // It's up to us to close the request (although it will time out).
  8.     //
  9.     req.abort();

  10.     //
  11.     // It's also up to us to emit an error so primus can clean up.
  12.     //
  13.     primus.socket.emit('error', 'authorization failed: ' + res.statusCode);
  14.   });
  15. });
  16. ```

If you want to read the response body then you can do something like this:

  1. ``` js
  2. primus.on('outgoing::open', function () {
  3.   primus.socket.on('unexpected-response', function (req, res) {
  4.     console.error(res.statusCode);
  5.     console.error(res.headers['www-authenticate']);

  6.     var data = '';

  7.     res.on('data', function (v) {
  8.       data += v;
  9.     });

  10.     res.on('end', function () {
  11.       //
  12.       // Remember error message is in the 'error' property.
  13.       //
  14.       primus.socket.emit('error', new Error(JSON.parse(data).error));
  15.     });
  16.   });
  17. });
  18. ```

If unexpected-response isn't caught (because the WebSocket transformer isn't
being used or you don't listen for it) then you'll get an error event:

  1. ``` js
  2. primus.on('error', function error(err) {
  3.   console.error('Something horrible has happened', err.stack);
  4. });
  5. ```

As noted above, err won't contain any details about the authorization failure
so you won't be able to distinguish it from other errors.

Broadcasting


Broadcasting allows you to write a message to every connected Spark on your server.
There are 2 different ways of doing broadcasting in Primus. The easiest way is to
use the Primus#write method which will write a message to every connected user:

  1. ``` js
  2. primus.write('message');
  3. ```

There are cases where you only want to broadcast a message to a smaller group of
users. To make it easier to do this, we've added a Primus#forEach method which
allows you to iterate over all active connections.

  1. ``` js
  2. primus.forEach(function (spark, id, connections) {
  3.   if (spark.query.foo !== 'bar') return;

  4.   spark.write('message');
  5. });
  6. ```

The method can be also used asynchronously. To enable the asynchronous iteration
you have to call Primus#forEach with two arguments. The first is the iterator
function that is called on every step. The iterator is called with a connection
from the list and a callback for when it has finished. The second argument is
the main callback and is called when the iteration has finished.

  1. ``` js
  2. primus.forEach(function (spark, next) {
  3.   //
  4.   // Do something and call next when done
  5.   //
  6.   next();
  7. }, function (err) {
  8.   console.log('We are done');
  9. });
  10. ```

There are also cases where you want to select a single Spark. To do this you
can use the Primus#spark method.

  1. ``` js
  2. // Get a spark by its id
  3. var spark = primus.spark(id);

  4. spark.write('message');
  5. ```

This method returns a Spark or undefined if the given id doesn't match any
of the active Spark ids on the server.

Destruction


In rare cases you might need to destroy the Primus instance you've created. You
can use the primus.destroy() or primus.end() method for this. This method
accepts an Object which allows you to configure the destruction process:

- close Close the HTTP server that Primus received. Defaults to true.
- reconnect Automatically reconnect the clients. Defaults to false.
- timeout Close all active connections and clean up the Primus instance after
  the specified amount of timeout. Defaults to 0.

The timeout is especially useful if you want gracefully shutdown your server but
really don't want to wait an infinite amount of time.

  1. ``` js
  2. primus.destroy({ timeout: 10000 });
  3. ```

Events


Primus is built upon the Stream and EventEmitter interfaces. This is a summary
of the events emitted by Primus.

Event                 | Usage       | Location      | Description
outgoing::reconnect | private     | client        | Transformer should reconnect.
reconnect scheduled | public  | client        | We're scheduling a reconnect.
reconnect           | public  | client        | Reconnect attempt is about to be made.
reconnected         | public  | client        | Successfully reconnected.
reconnect timeout   | public  | client        | Reconnect attempt took too much time.
reconnect failed    | public  | client        | Failed to reconnect.
timeout             | public  | client        | Failed to connect to server.
outgoing::open      | private     | client/spark  | Transformer should connect.
incoming::open      | private     | client/spark  | Transformer has connected.
open                | public  | client        | Connection is open.
destroy             | public  | client        | The instance has been destroyed.
incoming::error     | private     | client        | Transformer received an error.
error               | public  | client/spark  | An error happened.
incoming::data      | private     | client/server | Transformer received data.
outgoing::data      | private     | client/spark  | Transformer should write data.
data                | public  | client/spark  | We received data.
incoming::end       | private     | client/spark  | Transformer closed the connection.
outgoing::end       | private     | client/spark  | Transformer should close connection.
end                 | public  | client/spark  | The connection has ended.
close               | public  | client/server | The connection has closed, we might reconnect. / The server has been destroyed.
connection          | public  | server        | We received a new connection.
disconnection       | public  | server        | We received a disconnection.
initialised         | public  | server        | The server is initialised.
plugin              | public  | server        | A new plugin has been added.
plugout             | public  | server        | A plugin has been removed.
incoming::ping      | private     | client        | We received a ping message.
outgoing::ping      | private     | spark         | We're sending a ping message.
incoming::pong      | private     | spark         | We received a pong message.
outgoing::pong      | private     | client        | We're sending a pong message.
heartbeat           | public  | spark         | We've received a response to a heartbeat.
online              | public  | client        | We've regained a network connection.
offline             | public  | client        | We've lost our internet connection.
log                 | public  | server        | Log messages.
readyStateChange    | public  | client/spark  | The readyState has changed.
outgoing::url       | private     | client        | The options used to construct the URL.

As a rule of thumb assume that every event that is prefixed with incoming:: or
outgoing:: is reserved for internal use only and that emitting such events your
self will most likely result in chaos and destruction.

To make it easier for developers to emit events on Primus itself, we've added a
small helper function that checks if the event you want to emit is reserved for
Primus only. This would be all incoming:: and outgoing:: prefixed events and
the events listed above. This method is called `.reserved()` and it's
implemented on the Spark:

  1. ``` js
  2. primus.on('connection', function connection(spark) {
  3.   spark.on('data', function (data) {
  4.     //
  5.     // Just imagine that we receive an array of arguments from the client which
  6.     // first argument is the name of the event that we need to emit and the
  7.     // second argument are the arguments for function.
  8.     //
  9.     if (spark.reserved(data.args[0])) return;

  10.     spark.emit.apply(spark, data.args[0]);
  11.   });
  12. });
  13. ```

But also the client:

  1. ``` js
  2. var primus = new Primus('http://example.bar');

  3. primus.on('data', function (data) {
  4.   if (primus.reserved(data.args[0])) return;

  5.   primus.emit.apply(primus, data.args);
  6. });
  7. ```

And of course the Primus instance as well.

Heartbeats and latency


Heartbeats are used in Primus to figure out if we still have an active, working
and reliable connection with the server. These heartbeats are sent from the
server to the client as shown in the following diagram.

  1. ```
  2.      client will disconnect
  3.        if not recv within
  4.           `pingTimeout`

  5.      primus:pong:{timestamp}
  6.     +----------------------+
  7.     |                      |
  8. +---v----+            +---------+
  9. | server |            |  client |
  10. +--------+            +----^----+
  11.     |                      |
  12.     +----------------------+
  13.      primus:ping:{timestamp}

  14.       sent at `pingInterval`
  15.       server will disconnect
  16.       if no response since
  17.            last ping
  18. ```

The heartbeat message that we send over the connection is
`primus::ping::`. Upon receipt of this message, the client will sendback a `primus::pong::` message with the same `` it
received from the server.
This allows to calculate the latency between messages by simply getting
the `` and comparing it with the local time.

Supported Real-time Frameworks


The following transformers/transports are supported in Primus:

BrowserChannel


BrowserChannel was the original technology that GMail used for their real-time
communication. It's designed for same domain communication and does not use
WebSockets. To use BrowserChannel you need to install the browserchannel
module:

  1. ```
  2. npm install browserchannel --save
  3. ```

And tell Primus that you want to use browserchannel as transformer:

  1. ``` js
  2. var primus = new Primus(server, { transformer: 'browserchannel' });
  3. ```

The browserchannel transformer comes with built-in node client support and can be
accessed using:

  1. ``` js
  2. var Socket = primus.Socket
  3.   , socket = new Socket('url');
  4. ```

Please note that you should use at least version 1.0.6 which contains support
for query strings.

Engine.IO


Engine.IO is the low level transport functionality of Socket.IO 1.0. It supports
multiple transports for creating a real-time connection. It uses transport
upgrading instead of downgrading which makes it more resilient to blocking
proxies and firewalls. To enable engine.io you need to install the engine.io
module:

  1. ```
  2. npm install engine.io --save
  3. ```

And tell Primus that you want to use engine.io as transformer:

  1. ``` js
  2. var primus = new Primus(server, { transformer: 'engine.io' });
  3. ```

If you want to use the client interface inside of Node.js you also need to
install the engine.io-client:

  1. ```
  2. npm install engine.io-client --save
  3. ```

And then you can access it from your server instance:

  1. ``` js
  2. var Socket = primus.Socket
  3.   , socket = new Socket('url');
  4. ```

Faye


Faye is a WebSocket only transformer. It uses the faye-websocket module which
is part of the Faye project and supports all
protocol specifications. To use this you need to install the faye-websocket
module:

  1. ```
  2. npm install faye-websocket --save
  3. ```

And tell Primus that you want to use faye as transformer:

  1. ``` js
  2. var primus = new Primus(server, { transformer: 'faye' });
  3. ```

The faye transformer comes with built-in node client support and can be
accessed using:

  1. ``` js
  2. var Socket = primus.Socket
  3.   , socket = new Socket('url');
  4. ```

SockJS


SockJS is a real-time server that focuses on cross-domain connections and does
this by using multiple transports. To use SockJS you need to install the
sockjs module:

  1. ```
  2. npm install sockjs --save
  3. ```

And tell Primus that you want to use sockjs as transformer:

  1. ``` js
  2. var primus = new Primus(server, { transformer: 'sockjs' });
  3. ```

If you want to use the client interface inside of Node.js you also need to
install the sockjs-client module:

  1. ```
  2. npm install sockjs-client --save
  3. ```

And then you can access it from your server instance:

  1. ``` js
  2. var Socket = primus.Socket
  3.   , socket = new Socket('url');
  4. ```

uws


uws is a WebSocket only transformer. It uses the uws module which is probably
the fastest WebSocket server available in Node.js. To use uws you have to
install the uws module:

  1. ```
  2. npm install uws --save
  3. ```

And tell Primus that you want to use uws as transformer:

  1. ``` js
  2. var primus = new Primus(server, { transformer: 'uws' });
  3. ```

If you want to use the client interface inside of Node.js you also need to
install the ws module:

  1. ```
  2. npm install ws --save
  3. ```

And then you can access it from your server instance:

  1. ``` js
  2. var Socket = primus.Socket
  3.   , socket = new Socket('url');
  4. ```

WebSockets


If you are targeting a high end audience or maybe just need something for
internal uses you can use a pure WebSocket server. This transformer uses the
popular ws module which is battle tested and supports all protocol
specifications. To use WebSockets you need to install the ws module:

  1. ```
  2. npm install ws --save
  3. ```

And tell Primus that you want to use WebSockets as transformer:

  1. ``` js
  2. var primus = new Primus(server, { transformer: 'websockets' });
  3. ```

The WebSockets transformer comes with built-in node client support and can be
accessed using:

  1. ``` js
  2. var Socket = primus.Socket
  3.   , socket = new Socket('url');
  4. ```

As you can see from the examples above, it doesn't matter how you write the name
of the transformer, we just toLowerCase() everything.

Transformer inconsistencies


- BrowserChannel does not give you access to the remotePort of the incoming
  connection. So when you access spark.address the port property will be set
  to 1337 by default.
- BrowserChannel is the only transformer that does not support cross domain
  connections.
- BrowserChannel and SockJS are written in CoffeeScript and this can make
  debugging harder when their internals fail.
- Engine.IO and SockJS do not ship their client-side library with their server
  side component. We're bundling a snapshot of these libraries inside of Primus.
  We will always be targeting the latest version of these transformers when we
  bundle the library.

Parsers


In addition to support different frameworks we've also made it possible to use
custom encoding and decoding libraries. We're using JSON by default but you
could also use binary or EJSON for example (but these parsers need to be
supported by Primus, so check out the parser folder for examples). To specify
the parser to use you can supply a parser configuration option:

  1. ``` js
  2. var primus = new Primus(server, { parser: 'JSON' });
  3. ```

All parsers have an async interface for error handling.

Middleware


Primus has two ways of extending the functionality. We have plugins
but also support middleware. And there is an important difference between these.
The middleware layers allows you to modify the incoming requests before they
are passed in to the transformers. Plugins allow you to modify and interact with
the sparks. The middleware layer is only run for the requests that are handled
by Primus.

We support 2 kind of middleware, async and sync middleware. The main
difference between these kinds is that sync middleware doesn't require a
callback, it is completely optional. In Primus, we eat our own dog food. Various
of components in Primus are implemented through middleware layers:

- cors: Adds the Access Control headers.
- primus.js: It serves our primus.js client file.
- spec: It outputs the server specification (version, transformer, path).
- authorization: Our authorization handler, which is implemented as a middleware.
- no-cache: Add no-cache headers to every HTTP request.
- x-xss: Add X-XSS-Protection headers to every HTTP request.

Primus.use(name, fn, options, index)


The primus.use method is how you add middleware layers to your system. All
middleware layers need to be named. This allows you to also enable, disable and
remove middleware layers. The supplied function can either be a pre-configured
function that is ready to answer request/response or an unconfigured
middleware. An unconfigured middleware is a function with less then 2 arguments.
We execute this function automatically with Primus as context of the function
and optionally, the options that got provided:

  1. ``` js
  2. primus.use('name', function () {
  3.   var primus = this;

  4.   return function (req, res) {
  5.     res.end('foo');
  6.   }
  7. }, { foo: 'bar' });
  8. ```

As you can see in the example above, we assume that you return the actual
middleware layer. If you don't need any pre-configuration you can just supply
the function directly:

  1. ``` js
  2. // sync middleware
  3. primus.use('name', function (req, res) {

  4. });

  5. // async middleware
  6. primus.use('name', function (req, res, next) {
  7.   doStuff();
  8. });
  9. ```

You need to be aware that these middleware layers are running for HTTP requests
but also for upgrade requests. Certain middleware layers should only run for
HTTP or Upgrade requests. To make it possible you can add a http or upgrade
property to the middleware function and set it to false if you don't want it
to be triggered.

  1. ``` js
  2. primus.use('name', function () {
  3.   function middleware(req, res, next) {

  4.   }

  5.   middleware.upgrade = false; // Don't run this middleware for upgrades

  6.   return middleware;
  7. });
  8. ```

By default a new middleware layer is added after the previous one, but there
are cases where you need to add a middleware at a specified index in
the stack. To accomplish this you can use the optional 0 based index
argument.

  1. ``` js
  2. // add a middleware after the first two in the stack
  3. primus.use('name', function (req, res) {

  4. }, 2);
  5. ```

Primus.remove(name)


This method allows you to remove configured middleware. This works
for the middleware layers that you added but also the middleware layers that we
add by default. If you want to use a different way to serve the primus.js
file you can simply:

  1. ``` js
  2. primus.remove('primus.js');
  3. ```

And add your own middleware instead.

Primus.disable(name)


In addition to removing middleware layers, it's also possible to disable them so
they are skipped when we iterate over the middleware layers. It might be useful
to just disable certain middleware layers in production.

  1. ``` js
  2. primus.disable('name');
  3. ```

Primus.enable(name)


Of course, when you can disable middleware there also needs to be way to enable
them again. This is exactly what this method does. Re-enable a disabled
middleware layer.

  1. ``` js
  2. primus.enable('name');
  3. ```

Plugins


Primus was built as a low level interface where you can build your applications
upon. At it's core, it's nothing more than something that passes messages back
and forth between the client and server. To make it easier for developers to
switch to Primus we've developed a simple but effective plugin system that
allows you to extend Primus's functionality.

Plugins are added on the server side in the form of an Object:

  1. ``` js
  2. //
  3. // Require a plugin directly.
  4. //
  5. primus.plugin('name', require('metroplex'));

  6. //
  7. // Or supply it manually with the required object structure
  8. //
  9. primus.plugin('name', {
  10.   server: function (primus, options) {},
  11.   client: function (primus, options) {},
  12.   library: 'client side library'
  13. });
  14. ```

Or you can pass the plugin Object directly into the constructor:

  1. ``` js
  2. var primus = new Primus(server, { plugin: {
  3.   name: {
  4.     server: function (primus, options) {},
  5.     client: function (primus, options) {},
  6.     library: 'client side library'
  7.   }
  8. }})
  9. ```

And last but not least, you can also supply the constructor with a comma or
space separated list of plugin names which will be required automatically:

  1. ``` js
  2. var primus = new Primus(server, { plugin: 'metroplex, primus-emit' })
  3. ```

To remove added plugins you can use the plugout method:

  1. ``` js
  2. primus.plugin('name', require('metroplex'));
  3. primus.plugout('name'); // returns true/false indicating successful removal.
  4. ```

The server function is only executed on the server side and receives 2
arguments:

1. A reference to the initialised Primus server.
2. The options that were passed in the new Primus(server, { options })
   constructor. So the plugin can be configured through the same interface.

The client receives the same arguments:

1. A reference to the initialised Primus client.
2. The options that were passed in the new Primus(url, { options })
   constructor. So the plugin can be configured through the same interface.

The only thing you need to remember is that the client is stored in the library
using toString() so it cannot have any references outside the client's
closure. But luckily, there's a library property that will also be included on
the client side when it's specified. The library property should be an
absolute path to the library file.

Intercepting the connection events


The connection event is emitted using a async emitter. It checks if your
supplied event emitter function has extra callback function. When it detects
this it will wait with the execution of the other assigned listeners until the
callback has been called. Please note that the order of assigning event
listeners is still respected so if you've assigned a connection listener
before an async connection listener it will still be executed first.

  1. ``` js
  2. primus.on('connection', function (spark) {
  3.   console.log('first call, i have no spark.newproperty', spark.newproperty);
  4. });

  5. primus.on('connection', function (spark, next) {
  6.   longrunningasynmethod(spark.query, function (err, data) {
  7.     spark.newproperty = data;

  8.     console.log('second call, i added the new property');
  9.     next(err);
  10.   });
  11. });

  12. primus.on('connection', function (spark) {
  13.   console.log('third call, i can read the ', spark.newproperty);
  14. });
  15. ```

When an error argument is supplied it will automatically end the connection and
emit an error event on the spark. If you are coming from Socket.IO 1.0 >=,
this will basically work the same way as their middleware system.

Extending the Spark / Socket


The server has a .Spark property that can be extended. This allows you to
easily add new functionality to the socket. For example adding join room
function would be as easy as:

  1. ``` js
  2. primus.plugin('rooms', {
  3.   server: function (primus) {
  4.     var Spark = primus.Spark;

  5.     Spark.prototype.join = function () {
  6.       // implement room functionality.
  7.     };
  8.   }
  9. });
  10. ```

Transforming and intercepting messages


Intercepting and transforming messages is something that a lot of plugins
require. When you're building an EventEmitter plugin or something else you
probably don't want the default data event to be emitted but your custom
event. There are 2 different types of messages that can be transformed:

1. incoming These messages are being received by the server.
2. outgoing These messages are being sent to the client.

The transformer is available on both the client and the server and share, like
you would have expected the same identical API. Adding a new transformer is
relatively straightforward:

  1. ``` js
  2. primus.transform('incoming', function (packet) {
  3.   //
  4.   // The packet.data contains the actual message that either received or
  5.   // transformed.
  6.   //

  7.   // This would transform all incoming messages to foo;
  8.   packet.data = 'foo';

  9.   // If you are handling the message and want to prevent the `data` event from
  10.   // happening, simply `return false` at the end of your function. No new
  11.   // transformers will be called, and the event won't be emitted.
  12. });
  13. ```

These transformations can easily be done in the plugins:

  1. ``` js
  2. primus.plugin('name', {
  3.   server: function (primus) {
  4.     primus.transform('outgoing', function (packet) {
  5.       packet.data = 'foo';
  6.     });

  7.     primus.transform('incoming', function (packet) {
  8.       if (packet.data === 'foo') packet.data = 'bar';
  9.     });
  10.   },