Skip to content

Instantly share code, notes, and snippets.

@bnhansn
Last active October 22, 2016 15:13
Show Gist options
  • Save bnhansn/7282180a673bc526d86b7bfc59c09693 to your computer and use it in GitHub Desktop.
Save bnhansn/7282180a673bc526d86b7bfc59c09693 to your computer and use it in GitHub Desktop.
import { reset } from 'redux-form';
export function connectToChannel(socket, roomId) {
return (dispatch) => {
if (!socket) { return false; }
const channel = socket.channel(`rooms:${roomId}`);
// new function
channel.on('message_created', (message) => {
dispatch({ type: 'MESSAGE_CREATED', message });
});
channel.join().receive('ok', (response) => {
dispatch({ type: 'ROOM_CONNECTED_TO_CHANNEL', response, channel });
});
return false;
};
}
export function leaveChannel(channel) {
return (dispatch) => {
if (channel) {
channel.leave();
}
dispatch({ type: 'USER_LEFT_ROOM' });
};
}
// new function
export function createMessage(channel, data) {
return dispatch => new Promise((resolve, reject) => {
channel.push('new_message', data)
.receive('ok', () => resolve(
dispatch(reset('newMessage'))
))
.receive('error', () => reject());
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment