PubNub State Sync for Multiple Chat Clients
One good way to handle this situation is use the PubNub Storage & Playback Service for Data Management and Message State. Specifically the PUBNUB.history({...})
API call. You can create a "chatroom_status"
channel for every chat room (i.e. "chatroom_a_status"
, "chatroom_b_status"
).
Note: are you creating a unique moderator channel for each chat room, or a shared moderator channel? If the former, then the
"chatroom_status_n"
example above is basically the same thing.
Before the moderator changes the state of a chatroom, he can just call PUBNUB.history({...})
on the "chatroom_n_status"
channel to retrieve the last known state of the chatroom. If the status is "closed" the moderator will know another moderator closed the chat room (and if you pass the moderator ID, you could also track which moderator closed the channel).
You will be able to achieve State sync for chat may be accomplished by using PubNub Storage & Playback Service for Data Management.
Here is some example code for simple state management:
// Init PubNub
var pubnub = PUBNUB({ publish_key : 'demo', subscribe_key : 'demo' });
// Get State of Chat Room
function get_state( chat_room, callback ) {
pubnub.history({
channel : chat_room + '-state',
callback : function(msgs) { callback(msgs[0][0] || default_state) }
});
}
// Set State of Chat Room
function set_state( chat_room, state ) {
pubnub.publish({ channel : chat_room + '-state', message : state });
}
And you can use this like:
set_state( 'chatroom_a', { open : true } );
get_state( 'chatroom_a', function(state) { console.log(state) } );