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-reconnecting-socket

v1.2.0

Published

Typescript Redux socket

Downloads

46

Readme

Redux reconnecting socket

Redux middleware and reducer for creating a websocket connection.

Features

  • Send messages to the server simply by adding sendToServer: true to your actions.
  • Messages received from the server are automatically dispatched as actions.
  • Automatically tries to reconnect when the connection drops.
  • The state.connection.connected boolean is available in your store.
  • Optional: Use promises if you are expecting a response from the server after a certain message.

Installation

npm install --save redux-reconnecting-socket

Usage

1. Configure it in your middleware

import { applyMiddleware, createStore } from 'redux';
import { reduxReconnectingSocket } from 'redux-reconnecting-socket';

const store = createStore(
	rootReducer,
	applyMiddleware(
	    // ... other middleware
		reduxReconnectingSocket()
	)
);

2. Configure it in your root reducer (optional)

Only needed if you want to use the state.connection.connected boolean.

import { combineReducers } from 'redux';
import {
    reduxReconnectingSocketReducer,
    defaulReduxReconnectingSocketState
} from 'redux-reconnecting-socket';

export const rootReducer =  (history) => combineReducers({
    // ... other reducers
    connection: reduxReconnectingSocketReducer,
});

export const defaultAppState = {
    // ... other reducers
    connection: defaulReduxReconnectingSocketState,
};

3. Dispatch a socketConnect action in your app component

import { socketConnect } from 'redux-reconnecting-socket';

class App extends React.Component {
    componentDidMount() {
        const { dispatch  } = this.props;

        dispatch(socketConnect('ws://localhost:4000/ws'));
    }
    ...

4. Send messages to the server by adding sendToServer: true to your action types

dispatch({
    sendToServer: true,
    type: 'MY_ACTION',
    payload: {
        message: 'Hello server!'
    }
});

5. Listen to server messages in your own middlewares and reducers

Whenever the server sends a message over the web socket, the message is automatically dispatched as an action.

6. Emulate request/responses by adding promise: true to your action types

const request = dispatch({
    sendToServer: true,
    promise: true,
    type: 'MY_ACTION',
    payload: {
        message: 'Hello server!'
    }
});

request.then(
    (actionFromServer) => console.log('Success, received this action from the server', actionFromServer),
    (actionFromServer) => console.log('Received a message with type: \'ERROR\' from the server:', actionFromServer)
);

Whether the promise will be resolved or rejected depends on the error boolean in the message from the server:

{
    type: 'MY_SERVER_RESPONSE',
    requestId: 1, // the same request id that was sent to the server
    error: true, // true would cause the promise to be rejected
}

A numeric requestId will automatically be generated and added in the message to the server. When the server sends a message that includes the same requestId, the request promise will be completed.

Additionally, you have the option to cancel requests:

const request = dispatch({
    sendToServer: true,
    promise: true,
    type: 'MY_ACTION',
    payload: {
        message: 'Hello server!'
    }
});

request.cancel();

Three things will happen when you cancel a request:

  1. The message from the server with this requestId will be ignored. It will not be dispatched.
  2. The request promise will be rejected.
  3. A CANCEL_REQUEST message will be sent to the server:
{
    type: 'CANCEL_REQUEST',
    requestId: 1
}