Skip to content

Instantly share code, notes, and snippets.

@TheRusskiy
Last active October 9, 2016 12:17
Show Gist options
  • Save TheRusskiy/48e877e20b969cbf4ee107df3b2d98f4 to your computer and use it in GitHub Desktop.
Save TheRusskiy/48e877e20b969cbf4ee107df3b2d98f4 to your computer and use it in GitHub Desktop.
return {
type: actionType,
channel: channelName,
event: eventName,
data: data
};
import { configurePusher } from 'pusher-redux';
...
const options = { // options are... optional
authEndpoint: '/authenticate/me'
}
const store = configureStore(initialState);
configurePusher(store, API_KEY, options);
import { subscribe, unsubscribe } from 'pusher-redux';
import { NEW_ORDER } from '../pusher/constants';
...
export class MyPage extends React.Component {
constructor(props, context) {
super(props, context);
this.subscribe = this.subscribe.bind(this);
this.unsubscribe = this.unsubscribe.bind(this);
}
componentWillMount() {
this.subscribe();
}
componentWillUnmount() {
this.unsubscribe();
}
// upon receiving event 'some_event' for channel 'some_channe' pusher-redux is going to dispatch action NEW_ORDER
// you can bind multiple actions for the same event and it's gonna dispatch all of them
subscribe() {
subscribe('some_channel', 'some_event', NEW_ORDER);
}
unsubscribe() {
unsubscribe('some_channel', 'some_event', NEW_ORDER);
}
...
}
import { NEW_ORDER } from '../pusher/constants';
...
function orderReducer(state = initialState.orders, action) {
case NEW_ORDER:
return [...state, action.data.order];
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment