Skip to content

Instantly share code, notes, and snippets.

@LFSCamargo
Created December 27, 2018 17:54
Show Gist options
  • Save LFSCamargo/ed4a6481135540c7a76c0e71eb340962 to your computer and use it in GitHub Desktop.
Save LFSCamargo/ed4a6481135540c7a76c0e71eb340962 to your computer and use it in GitHub Desktop.
Presence manager with pubnub
// @flow
import React, { Component } from 'react';
import { View } from 'react-native';
import PubNubReact from 'pubnub-react';
import { connect } from 'react-redux';
import addContacts from '../actions/Contacts/AddContacts';
import sendRemoteMessage from '../actions/Messaging/AddRemoteMessages';
import { Contact } from '../reducers/Contacts/Contacts';
import { Messaging } from '../reducers/Messaging/Messaging';
import idx from 'idx';
type Props = {
username: string,
addContacts: (contacts: Contact[]) => void,
addMessage: (message: Messaging) => void,
};
type Occupant = {
state: any,
uuid: string,
};
type MessageResponse = {
actualChannel: string,
channel: string,
message:
| {
text: string,
}
| any,
publisher: string,
subscribedChannel: string,
subscription: any,
timetoken: string,
userMetadata: {
to: string,
from: string,
payload: {
text?: string,
description?: string,
candidate?: string,
hangup?: boolean,
hasReceivedDescription?: boolean,
},
},
};
type Response = {
channels: {
global: {
name: string,
occupacy: number,
occupants: Occupant[],
},
},
totalChannels: 1,
totalOccupancy: 2,
};
class PresenceManager extends Component<Props> {
constructor(props: Props) {
super(props);
this.pubnub = new PubNubReact({
publishKey: 'pubkey', // put your pubnub keys here
subscribeKey: 'subkey', // put your pubnub keys here
uuid: props.username,
});
this.pubnub.init(this);
}
pubnub: PubNubReact = {};
componentDidMount() {
const { username } = this.props;
this._init(username);
}
componentWillUnmount() {
this.pubnub.unsubscribe({ channels: ['global'] });
}
componentDidUpdate() {
const { username } = this.props;
this._init(username);
}
_init = (username: string) => {
this.pubnub.subscribe({
uuid: username,
channels: ['global'],
withPresence: true,
presenceTimeout: 8,
});
this.pubnub.getPresence('global');
this.pubnub.getMessage('global', (message: MessageResponse) => {
if (message.userMetadata.to === username) {
if (
!message.userMetadata.payload.candidate &&
!message.userMetadata.payload.description &&
!message.userMetadata.payload.text &&
!message.userMetadata.payload.hasReceivedDescription
) {
return;
}
this.props.addMessage({
from: message.publisher,
to: message.userMetadata.to,
payload: message.userMetadata.payload,
});
}
});
this.pubnub.hereNow({ channels: ['global'] }, (status, response: Response) => {
const occupants = idx(response, _ => _.channels.global.occupants) || [];
const newArr = occupants.map(element => {
return {
username: element.uuid,
isOnline: true,
};
});
this.props.addContacts(newArr);
});
};
render() {
return <View />;
}
}
const mapDispatchToProps = dispatch => ({
addContacts: (contacts: Contact) => dispatch(addContacts(contacts)),
addMessage: (message: Messaging) => dispatch(sendRemoteMessage(message)),
});
export default connect(
null,
mapDispatchToProps,
)(PresenceManager);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment