How do I get Private_Pub working with Rails 4?

1.9k views Asked by At

I'm trying to understand how to get private_pub working with Rails 4 using the Readme's Alternative Usage syntax. It's not working for me.

I created a brand new Rails 4 app with a single controller and index action (set as root route).

home_controller.rb

class HomeController < ApplicationController
  def index
    PrivatePub.publish_to "/messages/new", :chat_message => "Hello, world!"
  end
end

home.js.coffee

PrivatePub.subscribe "/messages/new", (data, channel) ->
  alert(data.chat_message)

I started the faye server with

rackup private_pub.ru -s thin -E production

I started the rails server, then went to the root page ("home#index"), and nothing is happening. The faye server is showing that nothing is going on. Based on my understanding, I "should" get an alert box popping up when I visit home#index, but nothing happens and I can't figure out why.

Any ideas? Thanks.

1

There are 1 answers

0
amp343 On

You may have already solved this, but I figured I'd chime in since I came across your question while trying to fix the same problem.

It seems you still need to <%= subscribe_to '/messages/new' %> in your index.html.erb or wahtever view, before calling PrivatePub.subscribe [...] in your CoffeeScript

Ryan Bates, in this old debug ticket, seems to imply that this is the case; he suggests error handling for the case of:

using PrivatePub.subscribe without subscribe_to call so messages are never received

... which sounds like the situation you described.

Hope this helps.