I have a mobile application where users can talk to each others via a chat (user to user only, not chatroom).
Today, everything is synchronous. We are considering switching the solution to something more "real time", maybe using PubNub or Pusher.
We are wondering about the best way to do it, given that our server will need to store every sent message.
What we have in mind :
My problem is regarding the server, today we have a Nginx/PHP backend. We would like our server to be able to listen all user channels as we need to store users sent messages on our own server.
For user to user communication, we are thinking to create our own protocol inside the messages. Eg. if user 111 wants to send "hello" to user 222, he could publish "to:222 hello", which would be retrieved by the server.
The server, upon receiving this command would push "from:111 hello" to his own channel (which would be 222).
The problem I see with this design is that our server would need to open as many channels as we have total users in our database.
I do not see a better way to do it.
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:
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.
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
http://pubsub.pubnub.com/v2/history/sub-key/demo/channel/my_channel?count=5
[["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"
));
?>
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.
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
As suggestion about how it can be done, try next thing:
So basically server subscribed only on one channel and receive messages from it, parse and send into recipient's channel (you don't have to be subscribed on channel into which you want to send message).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With