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.
相关问题
- Can we receive pubnub notifications by email?
- How to get total travelled distance and route from
- Pubnub Presence feature in Python (GAE)
- iOS / Swift : Receive callbacks from data streamin
- How can I list all pubnub channels with active sub
相关文章
- Handling Messages from PubNub History as a “Global
- How to Implement Pubnub Presence feature in Ruby
- Android chat application guidance using Parse and
- How to implement Pubnub Access Manager in swift
- Mobicents presence server. How to register softpho
- Using PubNub with React Native
- How to get the number of unread messages PubNub
- Pubnub chat application with storage
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
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
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
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.
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.
When an event occurs (such as a user joining) the output data will look like:
on a User Disconnect, the presence event is triggered as:
and potentially in the event of an error/timeout:
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:and the response object will look like:
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:And the response object data is identical to what is available to you in JavaScript.
Finally if you want to use the simple JSON REST interface provided by the PubNub Network, you can issue the following requests easily:
and the response output is identical:
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: