Integration of Pusher into Redux
You can download this by executing
npm -i S pusher-redux
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_channel' 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];
...
}
Pusher-redux dispatches actions of the following format:
return {
type: actionType,
channel: channelName,
event: eventName,
data: data
};
Sometimes you want to authenticate user for receiving pusher information, but you don't have user credentials yet. In this case you can do the following:
import { delayConfiguration } from 'pusher-redux';
...
const options = { // options are... optional
authEndpoint: '/authenticate/me'
}
const store = configureStore(initialState);
delayConfiguration(store, API_KEY, options);
And once user information is available
import { startConfiguration } from 'pusher-redux';
...
startConfiguration({ // pass options
auth: {
params: {
auth_token: user.authToken
}
}
});
If you want to use react-native then replace ALL imports of pusher-redux
with pusher-redux/react-native
e.g.
import { startConfiguration } from 'pusher-redux/react-native';
Pusher-redux accepts all the same options that pusher-js does
You are welcome to import more features from pusher-js
This code is released under the MIT License.