PubNub or Pusher and storing data on my own server

筅森魡賤 提交于 2019-11-30 21:07:51

As suggestion about how it can be done, try next thing:

  1. subscribe clients on their own channels
  2. subscribe server on some special channel with constant name
  3. if client A want to send message to client B it should send it into channel at which server subscribed before. Maybe you will have to send it in special format, which will allow to identify sender and recipient.
  4. Server parse formatted message from client A and send it into the channel at which channel B subscribed.

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).

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!