Giter Club home page Giter Club logo

react-with-websocket's Introduction

React withWebSocket

Higher order components for better experience in React with websocket.

Dependabot Status

Usage (with WebSocket from browser)

First, create a WebSocket object (for example, ws) in your (lower as possible) common ancestor of Components that require data from WebSocket, and wrap its children with a <WebSocketContextProvider value={ws}>.

That will be the source of WebSocket for those Components.

For each Component, we need to:

  1. Subscribe to a WebSocket

  2. Provide it a WebSocket

For the first part, we will create a subscription using createSubscription

const withSubscription = createSubscription(
    initState,
    fallbackProps,
    mapStateToProps,
    mapMessageToState,
    mapEventToState,
)

The subscription has a state initialized with initState. When there are new message from WebSocket, you need to mapMessageToState.

It receive a message event, return new state (or a function that map previous state to new state).

Similarly when there are new event like open,close,error, you can mapEventToState but that is optional.

Then you mapStateToProps.

It receive current state (and optionally the WebSocket object) and map it to (some of) your Component's props.

If the props returned from mapStateToProps is falsey, then it will use the value from fallbackProps.

Finally you apply the subscription to Component.

const ComponentWithSubscription = withSubscription(Component)

And the second part, let's feed the result with a WebSocket:

const ComponentWithWebSocketSubscription = withWebSocket(Component)

Then, you can use ComponentWithWebSocketSubscription in place of Component with some props subscribed to WebSocket.

For convenient, createWebSocketSubscription works as createSubscription then withWebSocket.

FOSSA Status

react-with-websocket's People

Contributors

dependabot-preview[bot] avatar librazy avatar dependabot-support avatar dependabot[bot] avatar

Stargazers

赤琦 avatar h3h3da avatar  avatar 伊欧 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.