Primus

GitHub stars
@feathersjs/primus"">npm version
Changelog

  1. $ npm install @feathersjs/primus --save

The @feathersjs/primus module allows to call service methods and receive real-time events via Primus, a universal wrapper for real-time frameworks that supports Engine.IO, WebSockets, Faye, BrowserChannel, SockJS and Socket.IO.

Important: This page describes how to set up Primus server. The Primus client chapter shows how to connect to this server on the client and the message format for service calls and real-time events.

Configuration

Additionally to @feathersjs/primus your websocket library of choice also has to be installed.

  1. $ npm install ws --save

app.configure(primus(options))

Sets up the Primus transport with the given Primus options.

Pro tip: Once the server has been started with app.listen() or app.setup(server) the Primus server object is available as app.primus.

  1. const feathers = require('@feathersjs/feathers');
  2. const primus = require('@feathersjs/primus');
  3. const app = feathers();
  4. // Set up Primus with SockJS
  5. app.configure(primus({ transformer: 'ws' }));
  6. app.listen(3030);

app.configure(primus(options, callback))

Sets up the Primus transport with the given Primus options and calls the callback with the Primus server instance.

  1. const feathers = require('@feathersjs/feathers');
  2. const primus = require('@feathersjs/primus');
  3. const app = feathers();
  4. // Set up Primus with SockJS
  5. app.configure(primus({
  6. transformer: 'ws'
  7. }, function(primus) {
  8. // Do something with primus object
  9. }));
  10. app.listen(3030);

params

The Primus request object has a feathers property that can be extended with additional service params during authorization:

  1. app.configure(primus({
  2. transformer: 'ws'
  3. }, function(primus) {
  4. // Do something with primus
  5. primus.use('feathers-referrer', function(req, res){
  6. // Exposing a request property to services and hooks
  7. req.feathers.referrer = request.referrer;
  8. });
  9. }));
  10. app.use('messages', {
  11. create(data, params, callback) {
  12. // When called via Primus:
  13. params.referrer // referrer from request
  14. }
  15. });

params.provider

For any service method call made through a Primus socket params.provider will be set to primus. In a hook this can for example be used to prevent external users from making a service method call:

  1. app.service('users').hooks({
  2. before: {
  3. remove(context) {
  4. // check for if(context.params.provider) to prevent any external call
  5. if(context.params.provider === 'primus') {
  6. throw new Error('You can not delete a user via Primus');
  7. }
  8. }
  9. }
  10. });

params.query

params.query will contain the query parameters sent from the client.

Important: Only params.query is passed between the server and the client, other parts of params are not. This is for security reasons so that a client can’t set things like params.user or the database options. You can always map from params.query to params in a before hook.

params.connection

params.connection is the connection object that can be used with channels. It is the same object as req.feathers in a Primus middleware as shown in the params section.