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 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-wsat

v0.0.4

Published

Redux Websocket Action Transfer

Downloads

15

Readme

Redux Websocket Action Transfer

This package allows to create middleware that transfer redux actions between client and server via websocket.

Why?

With Redux we can think in this way: our UI actions that we need to save to store we can describe as redux-actions. We also have some UI-component logic, for example, that we can save only in UI Component State, but we don't need to save them to server. So let's talk about only those actions, that we want so save to the store and to the server.

And we familiar with constructions like crud.updateUser(...) in the different places in the code. Well, we use redux-saga and a lot of other great libs, but mostly, in my opinion, we need to just keep the action.

The main point of this project is moving redux actions to the next level of services interactions and use it like messaging protocol between different service components, not only frontend. It can be useful for realtime applications with microservice architecture.

For example, your app sends actions from client to relay that passes them into the Service Bus, where they handle by workers. Then workers send results to Service Bus like action again, and relay passes them to all clients, that need.

And on each service layer action types may be common or specified with conversions.

It also allows you to get rid of such this in your code: crud.updateUser(...), because all action that you would like to send on server will send automatically.

You can initialize WS-connection asynchronously by action type - pass it in options like: { actions: { INIT } } See counter for example.

Usage

The default data-flow is:

  1. UI produced some action;
  2. WSAT middleware;
  3. server handle that action and send to clients action that they need to dispatch;
  4. clients recieve action from server, dispatch and update UI.

This flow allows you to have one data-flow direction and have more consistent data, espacially for collaborative working.

ReduxWSAT takes two arguments:

  • WebSocket Initor (required). Returns socket or something that resolves onmessage and onclose (optional, for reconnect) and represent the interface like native WebSocket API:
    • { onmessage, onclose, onerror } - optional, for events handling from WSAT;
    • { send } - required, for message sending to server.
  • options. { retry: { timeout }, actions: { INIT }, helpers: { isWSAT, prepareAction, getAction, isClientFirst } }
    • retry: { timeout } By default retries to connect without timeout;
    • actions: { INIT } By default {}. INIT is action type for async socket initialization;
    • helpers: { isWSAT, prepareAction, getAction, isClientFirst }
      • isWSAT - checks action for server sending. By default send all actions but action.wsat === false
      • prepareAction - prepare action for sending to server. By default action => JSON.stringify({ action })
      • getAction - returns action that received from socket, that would be dispatched (if Boolean(result)). By default return { wsat: false, ...action }
      • isClientFirst - checks if you need to dispatch that action on client too. Be careful, you can dispatch this action twice, if server send this action back to author

Let's say we have this configureStore.js:

import { createStore, applyMiddleware, compose } from 'redux';

import WSAT from 'redux-wsat';

import rootReducer from '~/reducers';
import { wsConfig } from '~/config';

export default () => {
  const wsat = WSAT(() => {
    const socket = new WebSocket(wsConfig.url);

    socket.onerror = error => console.log('WS error', error);
    socket.onclose = () => console.log('WS connection closed');
    socket.onopen = () => console.log('WS connection established');

    return socket;
  });

  const store = createStore(rootReducer, {}, compose(
    applyMiddleware(wsat),
  ));

  return store;
};

And stateless server, that keeps websocket connections and send received messages to all clients:

wss.on('connection', ws =>
  ws.on('message', message =>
    wss.clients.forEach(client => client.send(message))));

So thats it - we send all actions from client to server and then transfer them for each client (includes author), where they dispatch and update clients store and UI.

You can also check counter example with async socket initialization and action dispatch in socket event listners