7.3.4 • Published 4 years ago

primus-client v7.3.4

Weekly downloads
13
License
MIT
Repository
github
Last release
4 years ago

Primus-client

This is a prebuilt primus client-side library with default options. It does not come pre-minified as that is out of the scope of this project.

Link to the full library

https://www.npmjs.com/package/primus

Example client-side code

// can be used with feathers (optional)
import feathers from '@feathersjs/feathers';
// can be used with feathers (optional)
import primusClient from '@feathersjs/primus-client';
import Primus from 'primus-client';
import set from 'lodash/set';

const protocol = `ws${location.protocol === 'https:' ? 's' : ''}:`;

const connectionString = `${protocol}//${location.hostname}`;

const socket = new Primus(connectionString, {
  protocol,
});

// can be used with feathers (optional)
const app = feathers();

// can be used with feathers (optional)
app.configure(primusClient(socket));

// you can add token for auth or other query params
socket.on('outgoing::url', function connectionURL(url) {
  set(url, 'query', `${url.query}&token=${token}`);
});

function executePrimusRequest(...args) {
  if (!args[2]) {
    set(args, 2, {});
  }

  socket.send(...args);
}

const data = {
  test: true,
};

// provides an optional callback to the request if you match the server response type and id like:
// spark.write({
//   type: 1,
//   data: [null, msg],
//   id,
// });
executePrimusRequest('someRequestType', data, (err, msg) => {});

// listen to custom server response messages like:
// spark.write({
//   type: 0,
//   data: ['someResponseType', msg],
// });
socket.on('someResponseType', (msg) => {});

The following options are built-in (can NOT be changed):

NameDescriptionDefault
authorizationAuthorization handlernull
pathnameThe URL namespace that Primus can own/primus
parserMessage encoder for all communicationJSON
transformerThe transformer we should use internallywebsockets
pluginThe plugins that should be applied{}
pingIntervalInterval at which heartbeats are sent30000
globalSet a custom client class / global namePrimus
compressionUse permessage-deflate / HTTP compressionfalse
maxLengthMaximum allowed packet size, in bytes10485760
transportTransformer specific configuration{}
idGeneratorCustom spark id generator functionundefined
originscors List of origins*
methodscors List of accepted HTTP methodsGET,HEAD,PUT,POST,DELETE,OPTIONS
credentialscors Allow sending of credentialstrue
maxAgecors Cache duration of CORS preflight30 days
headerscors Allowed headersfalse
exposedcors Headers exposed to the clientfalse