Coder Social home page Coder Social logo

react-pusher's Introduction

React Pusher Component

A community managed React Component for managing Pusher subscriptions.

Originally conceived and implemented by @ziahamza and @prayogoa.

Usage

You can get react-pusher from npm, just run npm install react-pusher --save.

  1. To use react-pusher, you need to hand over your instance of the pusher service first. Otherwise the Pusher React component can't receive messages:
import { setPusherClient } from 'react-pusher';
import Pusher from 'pusher-js';

const pusherClient = new Pusher({
  <your_config>...
});

setPusherClient(pusherClient);
  1. Then you simply mount the component, inside another component of yours. The pusher subscription will stay alive for as long as the component does. It subscribes to events when mounted, and cleans up hanging subscriptions when unmounted.

Here's an example of using pusher-react in combination with redux. Everytime we receive a push notification for channel "someChannel" and event "listChanged", the fetchList() action is dispatched.

import { fetchList } from './actions';
import store from '../../store';
import Pusher from 'react-pusher';

const SomeList = ({ items }) => (
  <div>
    <ul>
      {items.map((item) => { <span>{item}</span> })}
    </ul>
    <Pusher
      channel="someChannel"
      event="listChanged"
      onUpdate={() => store.dispatch(fetchList())}
    />
  </div>
);

Rationale

We use pusher at rainforest. Previously our management of pusher notifications was wrapped in it's own service. A singleton, instantiated at app-startup.

Then we noticed a pattern - subscriptions to push notifications are tied to the lifecycle of components that make use of these subscriptions. This is the patttern: In 99% of cases:

  1. We want to subscribe to a pusher event stream when a component is mounted.
  2. We want to unsubscribe above pusher stream when given component is unmounted.

It made sense to move the management of pusher subscriptions into a React component, so we don't have to manage it ourselves.

react-pusher's People

Contributors

eddiemoore avatar juliankrispel avatar mdamien avatar prayogoa avatar teameh avatar ukd1 avatar ziahamza avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-pusher's Issues

React Native Support

Given the relatively simple outlined structure here it seems adding in support for React Native should be supported. Currently trying to instantiate a Pusher component inside a React Native tree throws the following exception:

Invariant Violation: Expected a component class, got [Object object].

I presume this is because of the "noscript" tag returned through render(). Is there any way to detect an RN environment and return an empty View instead (if that is indeed the error)?

Subscription after channel/event props update not working correctly

Just got this error today :

  • When channel/event props evolve in time, subscription to the new channel is not done.

I forked project and played with it : switching bind and unbind methods in componentWillReceiveProps seems to fix this issue in my case.

I made a PR with this fix.

Private channels?

Possibly a dumb question, but does this work with private channels? For some reason I can't seem to get it to subscribe / listen when using a private channel.

Undefined $this within pusher-js when rendering Pusher component

When I try to render a Pusher component, I receive this error:

TypeError: Cannot set property 'key' of undefined

Pusher
node_modules/pusher-js/dist/web/pusher.js:155

  152 |   logger_1["default"].warn("You should always specify a cluster when connecting. " + suffix);
  153 | }
  154 | 
> 155 | this.key = app_key;
      | ^  156 | this.config = Collections.extend(DefaultConfig.getGlobalConfig(), options.cluster ? DefaultConfig.getClusterConfig(options.cluster) : {}, options);
  157 | this.channels = factory_1["default"].createChannels();
  158 | this.global_emitter = new dispatcher_1["default"]();

My implementation:

import Pusher from 'pusher-js';
import { setPusherClient } from 'react-pusher';
    constructor(props){
        super(props);
        this.pusherClient = new Pusher("bf5df34b3b535a270b0a", {
            cluster: "us2",
        });
    }
    render(){
        setPusherClient(this.pusherClient);
        return (
            <Flexbox>
                <Pusher
                    channel="someChannel"
                    event="listChanged"
                />
            </Flexbox>
        )
    }

I'm assuming this is due to instantiating setPusherClient incorrectly?

Following the instructions causes this error

Uncaught Error: you must set a pusherClient by calling setPusherClient

My code is simply this

import { setPusherClient } from 'react-pusher';
import Pusher from 'react-pusher';

const pusherClient = new Pusher('xxxx1234567890xxxx', {encrypted: (document.location.protocol == 'https:')});
setPusherClient(pusherClient);

It's complaining about not having setPusherClient as soon as I define the variable.

React Warning -manually calling a React.PropTypes

Warning: You are manually calling a React.PropTypes validation function for the `channel` prop on `t`. This is deprecated and will not work in the next major version. You may be seeing this warning due to a third-party PropTypes library. See https://fb.me/react-warning-dont-call-proptypes for details.

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.