Giter Club home page Giter Club logo

react-with-actioncable's Introduction

react-with-actioncable

A higher order component (like redux's connect) to connect our components to ActionCable.

This contains assumptions on your channel behavior (for example, regarding which connection is rejected) so use at your own risk, or fork it if needed.

This is used with react-actioncable-provider so you need to install it too.

yarn add react-actioncable-provider

Usage

import ActionCable from 'actioncable'
import ActionCableProvider from 'react-actioncable-provider'

<ActionCableProvider cable={ActionCable.createConsumer(WEBSOCKET_URL)}>
  <App />
</ActionCableProvider>

For components that need to subscribe to channels, we connect it like this:

import withActionCable from 'react-with-actioncable'

const SomeComponent = (props) => {
  const {
    name,
    helloFunction
  } = props

  return (
    <div>
      {name && `Hello ${name}`!}
      <Button onPress={helloFunction} />
    <div/>
  )
}

export default withActionCable({
  channel: 'HelloChannel', // The channel that we will subscribe to.
  // Parameters that we pass to the channel. Props are passed to it from the component
  params: (props) => ({ name: 'foo' }),
  // tell the Hoc if we want to connect the component automatically or if we want to
  // connect to it manually using the provided props.cable.connectToChannel(params) function
  autoConnect: true,
  // this is called after we connect to the channel
  onConnect: (channel) => {},
  // this is called after we disconnect to the channel
  onDisconnect: () => {},
  // this is called if the server rejects our connection
  onReject: () => {},
  // this is called when we receive new data from the channel.
  // data is from the channel, props come from the props passed to the component.
  // this function should return an object, in which this will be passed as props to the component.
  onReceive: (data, props) => {
    return {
      name: data.name
    },
  },
  // this contains functions that we are going to use to broadcast to the channel
  // these will be passed down as props to the component as well
  broadcasters: {
    // it should return an object. the channel now will receive data containing { name: name }
    helloFunction: (name) => {
      return {
        name
      }
    }
  },
  // this contains functions that are also instance methods for the actioncable channel.
  // these will be passed down as props to the component.
  serverMethods: {
    foo: (bar) => {
      // it should return an object. the channel will now call on HelloChannel#foo, with
      // { bar: bar } as data
      return {
        bar
      }
    }
  }
})(SomeComponent)

We can also pass this function to the compose function if we want to use it with redux or apollo.

import { connect } from 'react-redux'
import { compose, graphql } from 'react-apollo'
import { withActionCable } from '../components/withActionCable'
import Component from './components/Component'

export default compose(
  graphql(),
  connect(mapState, mapDispatch),
  withActionCable({ channel: 'Foobar' })
)(Component)

Todo

  • Tests

License

MIT

react-with-actioncable's People

Contributors

siegcollado avatar

Watchers

James Cloos avatar  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.