Reputation: 197
I am trying to create a chat application using pubnub api in javascript for first time
Below described is the logic i created for implementing a chat
User A is subscribed to channel "talktoA" and "ourPublicChannel" User B is subscribed to channel "talktoB" and "ourPublicChannel"
When a User A want to talk to User B User A will a message to channel "talktoB" as user B is subscribed to channel "talktoB" User B will receive the message and vice versa
When users want to send broadcast message the users need to send message to channel "ourPublicChannel"
Following are the code for each operations
1. **Establish a Connection**
var pubnub = PUBNUB.init({
publish_key: 'pub-mypublishkey',
subscribe_key: 'sub-mysubkey',
uuid : me
});
2. **Publish Message to a Channel**
//Sending a private message
pubnub.publish({
channel: ['privatechannelofB'],
message: {
text: “Test Message to userB ”,
username: me
}
});
//Sending a broadcast message
pubnub.publish({
channel: ['publicchannel'],
message: {
text: “A Broadcast Message to all user”,
username: me
}
});
3. **Subscribe /Receive to a channel**
pubnub.subscribe({
channel: ['myprivatechannel','mypublichannel']
message: function(data) {
alert(data)//Test Message
}
});
4. **History of message**
pubnub.history({
channel: channelname,
callback: function(m){console.log(m)},
});
I need to confirm the following
How to retrieve the offline messages ? if user A send message to user B and user B is offline i need to show the offline messages? History api will give the full list of message but how sort it whether it is offline messages
Is the approach right?
Upvotes: 1
Views: 1143
Reputation: 3350
The Playback and Storage (history API) lets you to retrieve the message history of a channel up to 30 days retention.
When userA
sends a message to userB
, who is not connected to the internet, or the application is in the background, no problems, userB
will be able to retrieve every message that was sent to his channel in the past 30 days.
Otherwise there is no difference between "offline" and "online" messages. If a message was successfully sent, you can retrieve it with the history API.
You can also use the Mobile Push Gateway for push notifications, in this case your user will receive the message when the app is in the background state.
For the best user experience I'm combining the two stuff and had zero problems with receiving messages.
Upvotes: 0