PubNub Chat with Message History
Good news: you can easily write a chat app with many different channels assigned to each users and also save message history using PubNub's Real-time Network - High Availability Globally Distributed - Storage Service. With this service you are able to selectively load the messages directly on the Mobile/Web Client Device from the nearest Data Center for past message history, but also you can load the messages into your own server using out Storage Retrieval API. Let's see how this works with the following chat app:
Chat with History JavaScript Source Code
Subscribe to your USER_ID
Channel Name in order to receive messages from other users. Also load history from previous chats.
<script src="https://cdn.pubnub.com/pubnub.min.js"></script>
<script>(function(){
// INIT
var channel = 'USER_ID-123456';
var pubnub = PUBNUB.init({
subscribe_key : 'demo',
publish_key : 'demo'
});
// CHAT MESSAGE RECEIVER
function chat(message) {
// process chat message here...
}
// LOAD HISTORICAL MESSAGES
pubnub.history({
channel : channel, // USER_ID Channel
limit : 50, // Load Last 50 Messages
callback : function(msgs) { pubnub.each( msgs[0], chat ) }
});
// PUBNUB REAL-TIME NETWORK HA-TCP STREAM CONNECTION
// FOR RECEIVING INCOMING CHAT MESSAGES
pubnub.subscribe({
channel : channel, // USER_ID Channel
connect : connect, // Connected - Ready to Receive Messages
callback : chat // Callback Processor
});
})();</script>
That's the basics of the chat app on the mobile/web client app. Now you can easily load/save messages to/from a Global provider. Next you'll want to load these messages on your server from PHP using the PubNub REST interface.
Loading Stored Message via REST API on PHP Backend
You'll use the REST interface to collect previously posted messages as needed from you PHP Backend server. You may actually not need this step since the data is stored on PubNub's global Real-time Network where your messages are replicated to many geographic regions for reliability and high read/write performance.
PubNub Storage/History V2 REST API Doc - https://gist.github.com/stephenlb/d53f4cc3a891c03b478e
REST Request
http://pubsub.pubnub.com/v2/history/sub-key/demo/channel/my_channel?count=5
REST Response
[["Pub1","Pub2","Pub3","Pub4","Pub5"],13406746729185766,13406746845892666]
You can also use the PubNub PHP SDK to help with some of the complexities. You can find the PubNub PHP SDK here: https://github.com/pubnub/php and load history with this example:
<?php
$pubnub = new Pubnub(
"demo", ## PUBLISH_KEY
"demo", ## SUBSCRIBE_KEY
"", ## SECRET_KEY
false ## SSL_ON?
);
$history_data = $pubnub->history(array(
'channel' => $channel,
'count' => 100,
'end' => "13466530169226760"
));
?>
More Details about Storage REST API on PubNub
Please follow this link to dive further in depth with PubNub Storage API: https://gist.github.com/stephenlb/d53f4cc3a891c03b478e - This guide will help answer additional details regarding storage REST API.
More of a Full GUI Chat Client using History
The following is a group-chat which will help you get started, it is written using Bootstrap CSS Framework - https://github.com/pubnub/real-time-stocks/#simple-embedded-chat-application