Giter VIP home page Giter VIP logo

redux-ws's Introduction

[DEPRECATED] Redux WebSockets

Use redux-thunk instead. Since v2.1.0 identical functionality can be achieved with that package.

This article outlines an example of how to use redux-thunk with WebSockets. The implementation can be found in a Github repo, starting on this file. The example uses socket.io, but using web sockets directly should be similar.


npm version

WebSockets middleware for Redux. If you ever used Redux Thunk you already know how to use this.

What Does it Do?

Redux WebSockets exposes a socket connection to any action creator that returns a function instead of a plain object. This way you can easily 'reach' your long-living socket from any thunk that goes through Redux.

Usage

Write action creators that return functions instead of objects. Those functions will be immediately called with an object containing the socket, dispatch, and getState as the argument.

Note that this is a bit different from Redux Thunk โ€” you get passed on object, not dispatch/getState/socket directly. This is done to make it easier to reach either of them, without having to 'walk over' the parameters every time. I highly recommend using destructuring to get to the right parts of the object. See below.

// An action creator returns a function that gets passed an object containing
// the socket, dispatch, and getState as an argument. I am using ES2015
// destructuring to take socket and dispatch from this object and
// subsequently use it.
function subscribeToUpdates() {
  return ({ socket, dispatch }) => {
    socket.on('update', dispatch(reactToUpdate()));
  };
}

// Regular Redux action creator...
function reactToUpdate() {...}

Installation

Install from npm.

npm install --save redux-ws

Instantiate, pass the middleware creator your socket, and add to your middleware stack.

import { createStore, applyMiddleware } from 'redux';
import createSocketMiddleware from 'redux-ws';
import io from 'socket.io-client';
import reducer from './reducers/index';

const socketMiddleware = createSocketMiddleware(io('http://example.com/socket'));

const store = createStore(
  reducer,
  applyMiddleware(socketMiddleware)
);

// Now just write your actions...

The above example uses socket.io, but I don't see any reason this middleware couldn't be used with other WebSockets libraries โ€” all it does is exposes your socket process to thunks. Check out the source.

redux-ws's People

Contributors

arturmuller avatar mdvanes avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.