In my chat room there can be multiple moderators in a room at same time.
Every moderator can open/close room from public at any time.
If a room is closed, no new members can access it, no need to care for those who are already in.
So a question, how do I correctly sync the chatroom open / closed status?
I was using a special moderator channel, every moderator page kept publishing it's state to it, and also was subscribed to it.
It works most of the time, but I believe there can be a sync issue, when I publish Closed state, and then get Open state from another moderator who hasn't yet received my publish update.
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) } );
Related
I have used pusher recently in my PHP laravel project and it is working fine.
What I know about pusher is that it is a real time layer between our server and client and creates web socket connection to our client browser.
I setup pusher in my application using below tutorial:
pusher integration with laravel
What I have created using pusher for my web application:
1.I have created a notification functionality. Where when one user add some data to database say when one user starts following other user a event is triggered and that event sends data to particulr channel say 'notification-channel' and in my js code I have subscribed to this channel. For that I have written below line of codes:
//instantiate a Pusher object with our Credential's key
var pusher = new Pusher('68fd8888888888ee72c', {
encrypted: true
});
//Subscribe to the channel we specified in our Laravel Event
var channel = pusher.subscribe('notification-channel');
//Bind a function to a Event (the full Laravel class)
channel.bind('App\\Events\\HelloPusherEvent', addMessage);
By using addMessage() function I display some data. Now I have put a check on client side so that only if logged in user is intended to receive this message by writing simple if condition. As I have sent intended user's id in within data from App\Events\HelloPusherEvent so I used this Id to display msg to specific users only.
But I think this is not right approach to use pusher for notifications or any other functionality. Further in my project I want to use pusher for displaying new news feeds on user's newsfeed without page refresh, where obviously few users will see those posts according to whom is posting that news post.
But How I will user pusher in a way that I don't need to implement if conditions on client side to stop displaying data.
Here my concern is that if I will keep sending data to all the active clients and put if conditions to filter data that will ultimately degrade my application.
My concerns:
If pusher sends data to multiple clients that is obviously all the active users then will it not cause overhead.
Is there any option to use channels to channelize data to intended users only.
As I am implementing pusher first time I have few doubts regarding its actual working so is there any blog which can help me to understand its real time working.
Let me know if my question is not clear and specific enough, I will elaborate it further.
Thanks in advance to all who will try to answer.
This Question pusher-app-client-events explained that we can create different channels for different users to send msg to only intended users.
I go through this FAQ and came to know that we can create unlimited channels for one registered APP.
Creating multiple channels won't cause any overhead.
Now if I want to send notification to user 1 then I would create a channel 'notificaton-channel-1' and would subscribe user 1 to same channel within my frontend code.
The event class that I am using within my PHP laravel project looks like below:
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
/**
* Just implement the ShouldBroadcast interface and Laravel will automatically
* send it to Pusher once we fire it
**/
class HelloPusherEvent extends Event implements ShouldBroadcast
{
use SerializesModels;
/**
* Only (!) Public members will be serialized to JSON and sent to Pusher
**/
public $message;
public $id;
public $for_user_id;
/**
* Create a new event instance.
* #param string $message (notification description)
* #param integer $id (notification id)
* #param integer $for_user_id (receiver's id)
* #author hkaur5
* #return void
*/
public function __construct($message,$id, $for_user_id)
{
$this->message = $message;
$this->id = $id;
$this->for_user_id = $for_user_id;
}
/**
* Get the channels the event should be broadcast on.
*
* #return array
*/
public function broadcastOn()
{
//We have created names of channel on basis of user's id who
//will receive data from this class.
//See frontend pusher code to see how we have used this channel
//for intended user.
return ['notification-channel_'.$this->for_user_id];
}
}
and on Frontend I subscribed to 'notification-channel-'+logged_in_user_id
//Subscribe to the channel we specified in our Laravel Event
//Subscribe user to the channel created for this user.
//For example if user's id is 1 then bind to notification-channel_1
var channel = pusher.subscribe('notification-channel_'+$('#logged_in_userId').val());
//Bind a function to a Event (the full Laravel class)
channel.bind('App\\Events\\HelloPusherEvent', addMessage);
This way we can send data to intended users only rather blocking data received by all the users by putting conditions in our client side code.
I think you should add User ID within Blade template directly, not using a form field:
var channel = pusher.subscribe('notification-channel_{{ Auth::id() }}');
I am building a slack bot using a third party service to handle responses based on inputs rather than just hard coding them into the bot. This service's API needs a client id & a conversation id to get the response. I found out that each time a slack bot receives a message, it creates a new message object each time so there isn't a way of keeping the clientID and conversation ID within the message object and have slack hold onto it.
rtm.on(RTM_EVENTS.MESSAGE, function(message // <-- new object each time the bot hears a message){
rtm.sendMessage('hello', message.channel);
});
So shortened down, does anyone know of a way to keep a conversation between a single user and the bot while holding onto some type of variable to hold the client and conversation ID?
You can store the message.user ID and track the conversation referring to that specific user. You will need to keep track of all ongoing conversations yourself. Something like this
rtm.on(RTM_EVENTS.MESSAGE, function(message // <-- new object each time the bot hears a message){
if(stored_conversations.indexOf(message.user) > -1){
//customize message depending on history
rtm.sendMessage('I remember you', message.channel);
}
});
Or, you could use Botkit - it manages bot-user conversations for you.
I'm looking to develop a chat application with Pubnub where I want to make sure all the chat messages that are send is been stored in the database and also want to send messages in chat.
I found out that I can use the Parse with pubnub to provide storage options, But I'm not sure how to setup those two in a way where the messages and images send in the chat are been stored in the database.
Anyone have done this before with pubnub and parse? Are there any other easy options available to use with pubnub instead of using parse?
Sutha,
What you are seeking is not a trivial solution unless you are talking about a limited number of end users. So I wouldn't say there are no "easy" solutions, but there are solutions.
The reason is your server would need to listen (subscribe) to every chat channel that is active and store the messages being sent into your database. Imagine your app scaling to 1 million users (doesn't even need to get that big, but that number should help you realize how this can get tricky to scale where several server instances are listening to channels in a non-overlapping manner or with overlap but using a server queue implementation and de-duping messages).
That said, yes, there are PubNub customers that have implemented such a solution - Parse not being the key to making this happen, by the way.
You have three basic options for implementing this:
Implement a solution that will allow many instances of your server to subscribe to all of the channels as they become active and store the messages as they come in. There are a lot of details to making this happen so if you are not up to this then this is not likely where you want to go.
There is a way to monitor all channels that become active or inactive with PubNub Presence webhooks (enable Presence on your keys). You would use this to keep a list of all channels that your server would use to pull history (enable Storage & Playback on your keys) from in an on-demand (not completely realtime) fashion.
For every channel that goes active or inactive, your server will receive these events via the REST call (and endpoint that you implement on your server - your Parse server in this case):
channel active: record "start chat" timetoken in your Parse db
channel inactive: record "end chat" timetoken in your Parse db
the inactive event is the kickoff for a process that uses start/end timetokens that you recorded for that channel to get history from for channel from PubNub: pubnub.history({channel: channelName, start:startTT, end:endTT})
you will need to iterate on this history call until you receive < 100 messages (100 is the max number of messages you can retrieve at a time)
as you retrieve these messages you will save them to your Parse db
New Presence Webhooks have been added:
We now have webhooks for all presence events: join, leave, timeout, state-change.
Finally, you could just save each message to Parse db on success of every pubnub.publish call. I am not a Parse expert and barely know all of its capabilities but I believe they have some sort or store local then sync to cloud db option (like StackMob when that was a product), but even if not, you will save msg to Parse cloud db directly.
The code would look something like this (not complete, likely errors, figure it out or ask PubNub support for details) in your JavaScript client (on the browser).
var pubnub = PUBNUB({
publish_key : your_pub_key,
subscribe_key : your_sub_key
});
var msg = ... // get the message form your UI text box or whatever
pubnub.publish({
// this is some variable you set up when you enter a chat room
channel: chat_channel,
message: msg
callback: function(event){
// DISCLAIMER: code pulled from [Parse example][4]
// but there are some object creation details
// left out here and msg object is not
// fully fleshed out in this sample code
var ChatMessage = Parse.Object.extend("ChatMessage");
var chatMsg = new ChatMessage();
chatMsg.set("message", msg);
chatMsg.set("user", uuid);
chatMsg.set("channel", chat_channel);
chatMsg.set("timetoken", event[2]);
// this ChatMessage object can be
// whatever you want it to be
chatMsg.save();
}
error: function (error) {
// Handle error here, like retry until success, for example
console.log(JSON.stringify(error));
}
});
You might even just store the entire set of publishes (on both ends of the conversation) based on time interval, number of publishes or size of total data but be careful because either user could exit the chat and the browser without notice and you will fail to save. So the per publish save is probably best practice if a bit noisy.
I hope you find one of these techniques as a means to get started in the right direction. There are details left out so I expect you will have follow up questions.
Just some other links that might be helpful:
http://blog.parse.com/learn/building-a-killer-webrtc-video-chat-app-using-pubnub-parse/
http://www.pubnub.com/blog/realtime-collaboration-sync-parse-api-pubnub/
https://www.pubnub.com/knowledge-base/discussion/293/how-do-i-publish-a-message-from-parse
And we have a PubNub Parse SDK, too. :)
I'd like to list all channels associated with a subscribe key that have active subscribers. Is there a way to do that with pubnub? I am using the JavaScript API if that makes any difference.
PubNub Where Now API
Return a list of channels, associated with a subscribe key, where subscribers exist.
PUBNUB.where_now( { uuid : 'optional-uuid' }, function(data) {
console.log(data);
} );
Note at the time of this posting, where_now() is only available in +3.6 SDK versions.
I was wondering how to implement user presence with PubNub in Rails apps, but I didn't find a complete guide to how to implement this feature in both server and client side.
PubNub Presence with Ruby and JavaScript
Get started easily with PubNub Presence in Ruby and JavaScript by following this short guide which connects the state between the two PubNub SDKs. First you'll want to make sure you have the latest PubNub Ruby GEM client SDK install on your server. But before we get into the coding aspect we can talk about what PubNub Presence actually is.
PubNub Presence Introduction
PubNub Presence allows you to ask the question "Who is there?" and receive back an answer in the form of a List of UserIDs and an occupancy count of who is currently online right now.
Usually you ask this question in the context of a PubNub Channel. User's connect to a PubNub Channel in order to receive a stream of data from the PubNub Network. You control the stream by way of Channels by publishing and subscribing to channels by any valid UTF-8 string. Sometimes you want to know the current state of the PubNub Channel by requesting the current activity and list of connected users on the channel. You can do this by using PubNub Presence feature which provides the answer you seek.
PubNub Presence SDKs
Let's get starte by listing the two starting steps of including/loading the GEM ans JavaScript SDKs for your target platforms (This time it's Ruby+JavaScript Combo).
Install PubNub Ruby GEM
sudo gem install pubnub
Next you will ensure that you are running one of the latest JavaScript SDKs on your JavaScript Client App (usually a mobile phone app or website app).
Include PubNub JavaScript Client SDK
<script src=http://cdn.pubnub.com/pubnub-3.4.2.min.js ></script>
Now that you have accessed the two necessary base SDK libs for Ruby and JavaScript, you are able to receive/transmit information freely over the PubNub Network. Next we'll talk about how you can easily receive presence events as they occur on your PubNub Channel and also how you can query directly for the current channel state with here_now() API.
PubNub Dev Console - Presence
Use the PubNub Developer's Console to Monitor Presence Events.
You can use the PubNub Developer's Console to watch Presence Events as they occur. You will be able to see the event payload in JSON form in the presence section which is shown in the following image:
There are three events "action"s you can receive including:
"join" - A new user joined the channel.
"leave" - A user left the channel.
"timeout" - A user dropped connection and timed out.
PubNub Presence with REST
PubNub Here Now
With PubNub Presence you have access to TWO REST routes. The easiest route is the here_now() route.
PubNub Presence with SDKs
Example Source Code in JavaScript
The following is an example of method in JavaScript to receive data on a Channel and also receive Presence Events for that channel as they occur in real-time. Also you will notice the paramaters that are passed to you in your Function Callback. This method allows you to receive a stream of data into your JavaScript Application. Note there is a second method to receive presence data (called here_now()) which we will cover a bit further down.
<script>(function(){
var pubnub = PUBNUB.init({
subscribe_key : 'demo'
});
pubnub.subscribe({
channel : "hello_world", // YOUR CHANNEL.
message : function( message, env, channel ) {}, // RECEIVE MESSAGE.
presence : function( message, env, channel ) { // PRESENCE EVENTS.
console.log( "Channel: ", channel );
console.log( "Join/Leave/Timeout: ", message.action );
console.log( "Occupancy: ", message.occupancy );
console.log( "User ID: ", message.uuid );
}
})
})();</script>
Example Source Code in Ruby
Here is Ruby Code which is the ruby method to receive Presence Events in real-time as they occur. You can process the stream or (Firehose) of events as they come in. Note there is a second method to receive presence data (called here_now()) which we will cover a bit further down.
require 'pubnub'
pubnub = Pubnub.new(
:publish_key => 'demo', # publish_key only required if publishing.
:subscribe_key => 'demo', # required
:secret_key => nil, # optional, if used, message signing is enabled
:cipher_key => nil, # optional, if used, encryption is enabled
:ssl => nil # true or default is false
)
## Receive Presence Events on a Channel
pubnub.presence(
:channel => :hello_world,
:callback => lambda { |event_data| puts(event_data) }
)
When an event occurs (such as a user joining) the output data will look like:
{"action":"join", "timestamp":1364261400, "uuid":"9d497a30-3af2-4b67-a6b3-82f254711c11", "occupancy":4}
on a User Disconnect, the presence event is triggered as:
{"action":"leave", "timestamp":1364251540, "uuid":"9d497a30-3af2-4b67-a6b3-82f254711c11", "occupancy":3}
and potentially in the event of an error/timeout:
{"action":"timeout", "timestamp":1364251540, "uuid":"9d497a30-3af2-4b67-a6b3-82f254711c11", "occupancy":3}
Here_Now in JavaScript
There is a here_now() function available to you that allows you to issue a single REST request to the PubNub Network that gets the Current state of a channel's connectivity. The request looks like:
<script>(function(){
var pubnub = PUBNUB.init({
subscribe_key : 'demo'
});
pubnub.here_now({
channel : 'hello_world',
callback : function (message) { console.log(message) }
});
})();</script>
and the response object will look like:
{"uuids":["754e58b3-a79b-4d91-8f6c-5d994e43a310","175c2c67-b2a9-470d-8f4b-1db94f90e39e","fafd273d-9be5-4049-a6ce-653c467f7c5d"],"occupancy":3}
Here_Now in Ruby
Just like in JavaScript the same function for here_now() is available in Ruby too. Here is the ruby syntax version:
require 'pubnub'
pubnub = Pubnub.new(
:publish_key => 'demo', # publish_key only required if publishing.
:subscribe_key => 'demo', # required
:secret_key => nil, # optional, if used, message signing is enabled
:cipher_key => nil, # optional, if used, encryption is enabled
:ssl => nil # true or default is false
)
pubnub.here_now(
:channel => :hello_world,
:callback => lambda { |event_data| puts(event_data) }
)
And the response object data is identical to what is available to you in JavaScript.
{"uuids":["754e58b3-a79b-4d91-8f6c-5d994e43a310","175c2c67-b2a9-470d-8f4b-1db94f90e39e","fafd273d-9be5-4049-a6ce-653c467f7c5d"],"occupancy":3}
Finally if you want to use the simple JSON REST interface provided by the PubNub Network, you can issue the following requests easily:
curl http://pubsub.pubnub.com/v2/presence/sub_key/demo/channel/hello_world
and the response output is identical:
{"uuids":["754e58b3-a79b-4d91-8f6c-5d994e43a310","175c2c67-b2a9-470d-8f4b-1db94f90e39e","fafd273d-9be5-4049-a6ce-653c467f7c5d"],"occupancy":3}
That's it! Super simple and easy to use PubNub Network Presence with Ruby on Rails and JavaScript. If you have any questions please contact PubNub directly and also visit the following links for more details:
PubNub Network Ruby SDK - https://github.com/pubnub/ruby
PubNub Network JavaScript SDK - https://github.com/pubnub/javascript#simple-example