5

I'm using private_pub to implement a one-to-one chat-like application.

Here is my story: as a user, I would like to receive a message when my partner leaves the chat – closes the window, etc.

Looking through the Faye Monitoring docs here is my attempt at binding on unsubscribe:


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

Faye::WebSocket.load_adapter('thin')

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

wts_pubsub = PrivatePub.faye_app

wts_pubsub.bind(:subscribe) do |client_id, channel|
puts "[#{Time.now}] Client #{client_id} joined  #{channel}"
end

wts_pubsub.bind(:unsubscribe) do |client_id, channel|
  puts "[#{Time.now}] Client #{client_id} disconnected from #{channel}"
  PrivatePub.publish_to channel, { marius_says: 'quitter' }
end

run wts_pubsub

but I keep getting timeouts: [ERROR] [Faye::RackAdapter] Timeout::Error

Prying into PrivatePub#publish_to, data holds what I expect both when I'm publishing from the Rails or the private_pub app, but the private_pub app keeps hanging.

How can I get publishing from private_pub to work?

Marius Butuc
  • 17,781
  • 22
  • 77
  • 111
MarcBalaban
  • 75
  • 2
  • 7
  • This thread is somewhat old; have you figured out what the problem is? I've been spending hours on this exact problem without much success. – Etienne Jul 30 '13 at 17:19

2 Answers2

0

Your second bind should be to disconnect event instead of unsubscribe.

Also, remember to fire off a Faye/PrivatePub disconnect event in your client side code when a browser window is closed.

Note: You might need to do this for all open sessions with the Faye server or just on a channel by channel basis based on chat application's design

In plain JS this might be something like:

window.onbeforeunload = functionThatTriggersFayeDisconnectEvent;

Sorry for not using proper markup, posting from mobile.

Marius Butuc
  • 17,781
  • 22
  • 77
  • 111
Jasdeep Singh
  • 3,276
  • 4
  • 28
  • 47
  • Both `disconnect` and `unsubscribe` events are automatically triggered when closing the tab – here is the [modified private_pub.rb](https://gist.github.com/mariusbutuc/8cdd6d71e7595a2fb814#file-private_pub-rb) and [its output](https://gist.github.com/mariusbutuc/8cdd6d71e7595a2fb814#file-private_pub-out). Doesn't seem to solve the OP's issue. – Marius Butuc May 01 '13 at 16:36
0

After hours of research and numerous attempts, this is the solution I found:

Replace PrivatePub.publish_to channel, { marius_says: 'quitter' } with:

system "curl http://localhost:9292/faye -d 'message={\"channel\":\"#{channel}\", \"data\":{\"channel\":\"#{channel}\",\"data\":{\"message\":{\"content\":\"#{client_id} disconnected from this channel.\"}}}, \"ext\":{\"private_pub_token\":\"ADD_APPROPRIATE_SECRET_HERE\"}}' &"

This will trigger an asynchronous request (curl + &) which will bypass the problem. Not the best fix, but it works.

Etienne
  • 251
  • 5
  • 15