Ruby on Rails - Faye Framework - private_pub

I use private_pub to implement a personalized 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 , my attempt to bind to 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

Getting started PrivatePub#publish_to, the data retains what I expect both when publishing from Rails and in the private_pub application, but the private_pub application continues to hang.

How do I make publishing from private_pub work?

+5
source share
2 answers

The second binding should be disconnectinstead unsubscribe.

, Faye/PrivatePub disconnect , .

. , Faye -.

JS - :

window.onbeforeunload = functionThatTriggersFayeDisconnectEvent;

, .

0

:

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

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\"}}' &"

(curl + &), . , .

0

All Articles