ryanb / private_pub

Handle pub/sub messaging through private channels in Rails using Faye.
MIT License
864 stars 228 forks source link

Find user's online status #110

Closed przbadu closed 8 years ago

przbadu commented 9 years ago

Hi, I am using private_pub 1.0.3 and able to use it in my localhost. Now I want to implement functionality to check if user online status is online or offline using this gem.

I am totally new to web sockets and found private_pub really easy to configure it so implemented it.....but Now I am stuck in the above mentioned problem. So, please let me know what is the proper solution for it.

I found this tutorial (http://www.ryanepp.com/blog/how-do-i-tell-if-a-user-is-online)[http://www.ryanepp.com/blog/how-do-i-tell-if-a-user-is-online] and I think that can solve my problem....I have configured redis server and tested it through console, it is all working.....Now all I want to do is to replace websocket-rails part of code with our own private_pub gem utilization.

UPDATE

I found these faye events:

bayeux.bind('disconnect', function(clientId) {
  // event listener logic
});

client.bind('transport:down', function() {
  // Fires when the connection is lost
});
client.bind('transport:up', function() {
  // Fires when the connection is established
});

But, how can I use and bind these events using private_pub gem

Thanks

przbadu commented 9 years ago

Hi After surfing for some time, I found the way to bind some faye callbacks() like this:

# private_pub.ru

# Run with: rackup private_pub.ru -s thin -E production
require "bundler/setup"
require "yaml"
require "faye"
require "private_pub"

# Only load rails environment if required
# we are calling sidekiq so it is required
require ::File.expand_path('../config/environment',  __FILE__)

Faye::WebSocket.load_adapter('thin')

PrivatePub.load_config(File.expand_path("../config/private_pub.yml", __FILE__), ENV["RAILS_ENV"] || "development")
# run PrivatePub.faye_app

# UPDATED CODE - 
# added functionality in gem
app = PrivatePub.faye_app

# subscribe - online
app.bind(:subscribe) do |client_id, channel|
  puts "Client subscribe: #{client_id}:#{channel}"

  if /\/user\/*/.match(channel)
    p "ok ****8"
    SubscribeClient.perform_async(client_id, channel)
  end
end

# unsubscribe - offline
app.bind(:unsubscribe) do |client_id, channel|
  puts "Client unsubscribe: #{client_id}:#{channel}"
  UnsubscribeClient.perform_async(client_id)
end

# disconnect - offline
app.bind(:disconnect) do |client_id|
  puts "Client disconnect: #{client_id}"
  UnsubscribeClient.perform_async(client_id)
end

run app

PROBLEM

Now, my problem is, when I close browser or browser tab where my app is running, the disconnect event fires up:

e.g:

    Client disconnect: 7q30kyezc4d68kjnyuik9quw42tqk2u

1) But when did subscribe and unsubscribe events call? unsubscribe is not calling, when I close browser / tab....Only disconnect is triggered. 2) Also, when is subscribe triggered. It did fired for first time and when I reconnect to application again, it is not triggering.

What I want now is, When user disconnect from the app, I will update that user to offline mode and if he come back to application, I have to update him to online mode again.....How can I acheive this??

inspire22 commented 9 years ago

Did you get this working? I'm interested if there's an easy integration as well...

sanjusoftware commented 9 years ago

please let me also know if you find a problem to your solution @przbadu