npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

redux-primus

v1.2.3

Published

Middleware for primus using primus-emitter

Readme

redux-primus

An opinionated connector between primus and redux.

Based on https://github.com/itaylor/redux-socket.io

Basically just changed emit -> send

Due to the way primus works, it requires you to source the primus client generated by the primus server. By bundling in a static version in your SPA or getting it from the server at runtime.

Also for now requires you to use https://github.com/cayasso/primus-emitter

Philosophy

primus client->server messages should should be sent by dispatching actions to redux's store, where the action is the payload.

primus server->client messages should be dispatched as actions when received.

How to use

Installation

npm install --save redux-primus

Example usage

This will create a middleware that sends actions to the server when the action type starts with "server/". When the primus spark receives a message of type 'action', it will dispatch the action to the store.

The result of running this code from the client is a request to the server and a response from the server, both of which go through the redux store's dispatch method.

Client side:

import { createStore, applyMiddleware } from 'redux';
import createPrimusMiddleware from 'redux-primus';
import primus from 'client';
let spark = primus('http://localhost:3000');
let primusMiddleware = createPrimusMiddleware(spark, "server/");
function reducer(state = {}, action){
  switch(action.type){
    case 'message':
      return Object.assign({}, {message:action.data});
    default:
      return state;
  }
}
let store = applyMiddleware(primusMiddleware)(createStore)(reducer);
store.subscribe(()=>{
  console.log('new client state', store.getState());
});
store.dispatch({type:'server/hello', data:'Hello!'});

Allowed criteria for action matching

When you create this middleware, you can configure how it detects that a given action should be sent to primus. This is done with the second parameter to createPrimusMiddleware.

You can pass either a prefix string that will be matched against the action.type:

let primusMiddleware = createPrimusMiddleware(spark, 'server/');

An array of strings that will will be used as allowed prefixes:

let primusMiddleware = createPrimusMiddleware(spark, [ 'post/', 'get/' ]);

Or a function that returns a truthy value if the action should be sent to primus:

let primusMiddleware = createPrimusMiddleware(spark, (type, action) => action.io);

Advanced usage

The default behavior is an "optimistic" update mode, where if an action matches the criteria you provided when you created the primus middleware, the middleware calls spark.send('action', action) and then passes the action to the next middleware in the chain.

If you want to change this behavior, you can provide your own execute function that allows you to decide what to do with the action that matched your criteria.

You do this by providing a function (action, emit, next, dispatch) as the execute property of the third parameter of createPrimusMiddleware

Example execute functions:

This is equivalent to the default execute function, so this is what will happen if you don't override it. Use something like this if you want optimistic updates of your state, where the action you dispatch goes both to the server and to the redux reducers.

import createPrimusMiddleware from 'redux-primus';
function optimisticExecute(action, emit, next, dispatch) {
  emit('action', action);
  next(action);
}
let primusMiddleware = createPrimusMiddleware(spark, "server/", { execute: optimisticExecute });

Here's a function that would make the middleware swallow all the actions that matched the criteria and not allow them to continue down the middleware chain to the reducers. This is easily used to make "pessimistic" updates of your state, by having the server respond by sending back an action type of the same type it was sent.

import createPrimusMiddleware from 'redux-primus';
function pessimisticExecute(action, emit, next, dispatch) {
  emit('action', action);
}
let primusMiddleware = createPrimusMiddleware(spark, "server/", { execute: pessimisticExecute });

Here's a function that would make the middleware dispatch an alternate action that could be used in a scenario where you want the optimistic updates to be very explicit. Here you would have actions of type server/<actionName> sent to the server, and also have another action optimistic/<actionName> dispatched as well with the same content.

import createPrimusMiddleware from 'redux-primus';
function optimisticExecute(action, emit, next, dispatch) {
  emit('action', action);
  const optimisticAction = {
    ...action,
    type: 'optimistic/' action.type.split('/')[1];
  }
  dispatch(optimisticAction);
}
let primusMiddleware = createPrimusMiddleware(spark, "server/", { execute: optimisticExecute });