All Projects → giantmachines → Redux Websocket

giantmachines / Redux Websocket

Licence: mit
A Redux middleware to handle WebSocket connections.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Redux Websocket

Django Channels React Multiplayer
turn based strategy game using django channels, redux, and react hooks
Stars: ✭ 52 (-77.59%)
Mutual labels:  middleware, websocket
Openremote
100% open-source IoT Platform - Integrate your assets, create rules, and visualize your data
Stars: ✭ 254 (+9.48%)
Mutual labels:  middleware, websocket
Echo
High performance, minimalist Go web framework
Stars: ✭ 21,297 (+9079.74%)
Mutual labels:  middleware, websocket
Http Proxy Middleware
⚡ The one-liner node.js http-proxy middleware for connect, express and browser-sync
Stars: ✭ 8,730 (+3662.93%)
Mutual labels:  middleware, websocket
Twig
Twig - less is more's web server for golang
Stars: ✭ 98 (-57.76%)
Mutual labels:  middleware, websocket
Joynr
A transport protocol agnostic (MQTT, HTTP, WebSockets etc.) Franca IDL based communication framework supporting multiple communication paradigms (RPC, Pub-Sub, broadcast etc.)
Stars: ✭ 124 (-46.55%)
Mutual labels:  middleware, websocket
Gin Web
由gin + gorm + jwt + casbin组合实现的RBAC权限管理脚手架Golang版, 搭建完成即可快速、高效投入业务开发
Stars: ✭ 107 (-53.88%)
Mutual labels:  middleware, websocket
Zan Proxy
An extensible proxy for PC/Mobile/APP developer
Stars: ✭ 1,727 (+644.4%)
Mutual labels:  middleware, websocket
Cloudtunes
Web-based music player for the cloud ☁️ 🎶 Play music from YouTube, Dropbox, etc.
Stars: ✭ 2,449 (+955.6%)
Mutual labels:  websocket
Mux
A powerful HTTP router and URL matcher for building Go web servers with 🦍
Stars: ✭ 15,667 (+6653.02%)
Mutual labels:  middleware
Cowlib
Support library for manipulating Web protocols.
Stars: ✭ 219 (-5.6%)
Mutual labels:  websocket
Voovan
Voovan是高性能异步通信、HTTP服务器和客户端通信、动态编译支持、数据库操作帮助类等工具的框架, 如果项目觉得不错, 请点一下 star, 谢谢
Stars: ✭ 221 (-4.74%)
Mutual labels:  websocket
Websocket
A fast, well-tested and widely used WebSocket implementation for Go.
Stars: ✭ 16,070 (+6826.72%)
Mutual labels:  websocket
Ayame
WebRTC Signaling Server Ayame
Stars: ✭ 218 (-6.03%)
Mutual labels:  websocket
Servr
A simple HTTP server in R
Stars: ✭ 228 (-1.72%)
Mutual labels:  websocket
Layim
基于HTML5 WebSocket的一款IM即时通讯软件,使用Gradle集成了Scala、SpringBoot、Spring MVC、Mybatis、Redis等,前端使用了LayIm框架
Stars: ✭ 218 (-6.03%)
Mutual labels:  websocket
Goku Api Gateway
A Powerful HTTP API Gateway in pure golang!Goku API Gateway (中文名:悟空 API 网关)是一个基于 Golang开发的微服务网关,能够实现高性能 HTTP API 转发、服务编排、多租户管理、API 访问权限控制等目的,拥有强大的自定义插件系统可以自行扩展,并且提供友好的图形化配置界面,能够快速帮助企业进行 API 服务治理、提高 API 服务的稳定性和安全性。
Stars: ✭ 2,773 (+1095.26%)
Mutual labels:  middleware
Router.cr
Minimum High Performance Middleware for Crystal Web Server.
Stars: ✭ 231 (-0.43%)
Mutual labels:  middleware
Twitch Js
A community-centric, community-supported version of tmi.js
Stars: ✭ 225 (-3.02%)
Mutual labels:  websocket
Spring Dubbo Service
微服务 spring dubbo项目:dubbo rpc;druid数据源连接池;mybatis配置集成,多数据源;jmx监控MBean;定时任务;aop;ftp;测试;Metrics监控;参数验证;跨域处理;shiro权限控制;consul服务注册,发现;redis分布式锁;SPI服务机制;cat监控;netty服务代理;websocket;disconf;mongodb集成;rest;docker;fescar
Stars: ✭ 224 (-3.45%)
Mutual labels:  websocket

redux-websocket codecov npm version npm

redux-websocket is a Redux middleware for managing data over a WebSocket connection.

This middleware uses actions to interact with a WebSocket connection including connecting, disconnecting, sending messages, and receiving messages. All actions follow the Flux Standard Action model.

Features

  • Written in TypeScript.
  • Interact with a WebSocket connection by dispatching actions.
  • Connect to multiple WebSocket streams by creating multiple middleware instances.
  • Handle WebSocket events with Redux middleware, integrate with Saga, and use reducers to persist state.
  • Automatically handle reconnection.

Installation

$ npm i @giantmachines/redux-websocket

Configuration

Configure your Redux store to use the middleware with applyMiddleware. This package exports a function to create an instance of a middleware, which allows for configuration options, detailed below. Furthermore, you can create multiple instances of this middleware in order to connect to multiple WebSocket streams.

import { applyMiddleware, compose, createStore } from 'redux';
import reduxWebsocket from '@giantmachines/redux-websocket';

import reducer from './store/reducer';

// Create the middleware instance.
const reduxWebsocketMiddleware = reduxWebsocket();

// Create the Redux store.
const store = createStore(reducer, applyMiddleware(reduxWebsocketMiddleware));

You may also pass options to the reduxWebsocket function.

Available options

interface Options {
  // Defaults to 'REDUX_WEBSOCKET'. Use this option to set a custom action type
  // prefix. This is useful when you're creating multiple instances of the
  // middleware, and need to handle actions dispatched by each middleware instance separately.
  prefix?: string;
  // Defaults to 2000. Amount of time to wait between reconnection attempts.
  reconnectInterval?: number;
  // Defaults to false. If set to true, will attempt to reconnect when conn is closed without error event
  // e.g. when server closes connection
  reconnectOnClose?: boolean;
  // Defaults to true. If set to true, will attempt to reconnect when conn is closed with error event
  reconnectOnError?: boolean;
  // Callback when the WebSocket connection is open. Useful for when you
  // need a reference to the WebSocket instance.
  onOpen?: (socket: WebSocket) => void;
  // Custom function to serialize your payload before sending. Defaults to JSON.stringify
  // but you could use this function to send any format you like, including binary
  serializer?: (payload: any) => string | ArrayBuffer | ArrayBufferView | Blob;
  // Custom function to deserialize the message data sent from the server. By default the
  // message data gets passed through as is.
  deserializer?: (message: any) => any;
  // Custom function to serialize the timestamp. The default behavior maintains the timestamp
  // as a Date but you could use this function to store it as a string or number.
  dateSerializer?: (date: Date) => string | number;
}

Usage

redux-websocket will dispatch some actions automatically, based on what the internal WebSocket connection. Some actions will need to be dispatched by you.

By default redux-websocket actions get dispatched with a timestamp as a Date object. This has caused some users to experience non serializable action warnings when using redux toolkit. If you encounter this problem you can either add a dateSerializer function to redux-websocket options or setup redux toolkit to ignore the actions.

User dispatched actions

These actions must be dispatched by you, however we do export action creator functions that can be used.

⚠️ If you have created your middleware with a prefix option, make sure you pass that prefix as the second argument to all of these action creators.


➡️ REDUX_WEBSOCKET::WEBSOCKET_CONNECT
Example:
import { connect } from '@giantmachines/redux-websocket';

store.dispatch(connect('wss://my-server.com'));

// You can also provide protocols if needed.
// See: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket
//
// Note that this function only allows passing an array of protocols, even though
// the spec allows passing a string or an array of strings. This is to support
// the prefix argument, in the case that you've prefixed your action names.
store.dispatch(connect('wss://my-server.com', ['v1.stream.example.com']));

// ...other ways to call this function:
store.dispatch(
  connect('wss://my-server.com', ['v1.stream.example.com'], 'MY_PREFIX')
);
store.dispatch(connect('wss://my-server.com', 'MY_PREFIX'));
Arguments:
  1. url (string): WebSocket URL to connect to.
  2. [protocols] (string[]): Optional sub-protocols.
  3. [prefix] (string): Optional action type prefix.

➡️ REDUX_WEBSOCKET::WEBSOCKET_DISCONNECT
Example:
import { disconnect } from '@giantmachines/redux-websocket';

store.dispatch(disconnect());
Arguments:
  1. [prefix] (string): Optional action type prefix.

➡️ REDUX_WEBSOCKET::WEBSOCKET_SEND
Example:
import { send } from '@giantmachines/redux-websocket';

store.dispatch(send({ my: 'message' }));
Arguments:
  1. message (any): Any JSON serializable value. This will be stringified and sent over the connection. If the value passed is not serializable, JSON.stringify will throw an error.
  2. [prefix] (string): Optional action type prefix.

Library dispatched actions

These actions are dispatched automatically by the middlware.

⬅️ REDUX_WEBSOCKET::OPEN

Dispatched when the WebSocket connection successfully opens, including after automatic reconnect.

Structure
{
    type: 'REDUX_WEBSOCKET::OPEN',
    meta: {
        timestamp: Date,
    },
}

⬅️ REDUX_WEBSOCKET::CLOSED

Dispatched when the WebSocket connection successfully closes, both when you ask the middleware to close the connection, and when the connection drops.

Structure
{
    type: 'REDUX_WEBSOCKET::CLOSED',
    meta: {
        timestamp: Date,
    },
}

⬅️ REDUX_WEBSOCKET::MESSAGE

Dispatched when the WebSocket connection receives a message. The payload includes a message key, which is JSON, and an origin key, which is the address of the connection from which the message was recieved.

Structure
{
    type: 'REDUX_WEBSOCKET::MESSAGE',
    meta: {
        timestamp: Date,
    },
    payload: {
        message: string,
        origin: string,
    },
}

⬅️ REDUX_WEBSOCKET::BROKEN

Dispatched when the WebSocket connection is dropped. This action will always be dispatched after the CLOSED action.

Structure
{
    type: 'REDUX_WEBSOCKET::BROKEN',
    meta: {
        timestamp: Date,
    },
}

⬅️ REDUX_WEBSOCKET::BEGIN_RECONNECT

Dispatched when the middleware is starting the reconnection process.

Structure
{
    type: 'REDUX_WEBSOCKET::BEGIN_RECONNECT',
    meta: {
        timestamp: Date,
    },
}

⬅️ REDUX_WEBSOCKET::RECONNECT_ATTEMPT

Dispatched every time the middleware attempts a reconnection. Includes a count as part of the payload.

Structure
{
    type: 'REDUX_WEBSOCKET::RECONNECT_ATTEMPT',
    meta: {
        timestamp: Date,
    },
    payload: {
        count: number,
    },
}

⬅️ REDUX_WEBSOCKET::RECONNECTED

Dispatched when the middleware reconnects. This action is dispached right before an OPEN action.

Structure
{
    type: 'REDUX_WEBSOCKET::RECONNECTED',
    meta: {
        timestamp: Date,
    },
}

⬅️ REDUX_WEBSOCKET::ERROR

General purpose error action.

Structure
{
    type: 'REDUX_WEBSOCKET::ERROR',
    error: true,
    meta: {
        timestamp: Date,
        message: string,
        name: string,
        originalAction: Action | null,
    },
    payload: Error,
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].