ryanb / private_pub

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

putting subscribe_to in js.erb template #91

Closed ajbraus closed 10 years ago

ajbraus commented 10 years ago

I want to start listening to a new private_pub faye channel after its first message is created. That means I want to put the subscribe_to tag inside of the messages#create.js.erb template, but the subscribe_to method outputs this:

<script type="text/javascript">PrivatePub.sign({DATA});</script>

the script inside the .js.erb template does not work.

any work around?

http://stackoverflow.com/questions/20983337/starting-a-private-pub-private-chat-room-rails-4

gregmolnar commented 10 years ago

You don't need the script tags in the js.erb file. Use just

PrivatePub.sign({DATA});
ajbraus commented 10 years ago

I tried that but it doesn't keep listening. It listens once, publish once, but then the next time I publish to the same channel it doesn't work.

PrivatePub.sign({ "server":"http://localhost:9292/faye/faye", "timestamp":1389913205, "channel": "/chat_room/2", "signature":"blahblahblah" });

I am creating this with ruby like this:

PrivatePub.sign({ "server":"http://localhost:9292/faye/faye", "timestamp":<%= Time.now.to_i %>, "channel": "<%= @chat_room.channel_path %>", "signature":"ae161991ecad6f49a179e90ea9e1b716202af937" });

publishing like this in create.js

<% publish_to @chat_room.channel_path do %> $('#messages').prepend("<%= j render partial: 'messages/message', :object => @message %>"); <% end %>

ajbraus commented 10 years ago

This is still not working. I think the problem is my "signature" - What should the signature value be?

gregmolnar commented 10 years ago

@ajbraus it should be the secret token.

ajbraus commented 10 years ago

Won't this be public then?

When I refresh and it is working it is not the secret token.

On Jan 23, 2014, at 12:00 AM, Greg Molnar notifications@github.com wrote:

@ajbraus it should be the secret token.

— Reply to this email directly or view it on GitHub.

gregmolnar commented 10 years ago

You are right. I forgot there is some hashing to do:

Digest::SHA1.hexdigest([secret_token, channel, timestamp].join)
ajbraus commented 10 years ago

Thanks! This is not working :o( is my syntax right?

"signature": "<%= Digest::SHA1.hexdigest([ ENV['PRIVATE_PUB_SECRET'], @chat_room.channel_path, Time.now.to_i ].join) %>"

~Adam Braus 608-770-0230

On Jan 23, 2014, at 8:42 AM, Greg Molnar notifications@github.com wrote:

Digest::SHA1.hexdigest([secret_token, channel, timestamp].join)

gregmolnar commented 10 years ago
<% timestamp = Time.now.to_i %>
PrivatePub.sign({ 
"server":"http://localhost:9292/faye/faye",
"timestamp":<% timestamp %>,
"channel": "<%= @chat_room.channel_path %>",
"signature": "<%= Digest::SHA1.hexdigest([ENV['PRIVATE_PUB_SECRET'], @chat_room.channel_path, timestamp].join) %>"
});

The timestamps has to match.

ajbraus commented 10 years ago

Good point!

But still not working. I cleaned it up even more and still not working:

<% timestamp = Time.now.to_i %> <% channel = @chat_room.channel_path %> <% signature = Digest::SHA1.hexdigest([ENV['PRIVATE_PUB_SECRET'], channel, timestamp].join) %>

PrivatePub.sign({ "server":"http://localhost:3000/faye/faye", "timestamp":<%= timestamp %>, "channel": "<%= channel %>", "signature": "<%= signature %>" });

When I compare it with what the subscribe_to method outputs there is no difference. Maybe the problem is in the signature logic? I've tried making channel a string and not a string. The pub secret is definitely right - I'm using dot-env gem to get env variables.

~Adam Braus 608-770-0230

On Jan 23, 2014, at 12:25 PM, Greg Molnar notifications@github.com wrote:

<% timestamp = Time.now.to_i %> PrivatePub.sign({ "server":"http://localhost:9292/faye/faye", "timestamp":<% timestamp %>, "channel": "<%= @chat_room.channel_path %>", "signature": "<%= Digest::SHA1.hexdigest([ENV['PRIVATE_PUB_SECRET'], @chat_room.channel_path, timestamp].join) %>" });

gregmolnar commented 10 years ago

You can check the signature logic here: https://github.com/ryanb/private_pub/blob/master/lib/private_pub.rb#L60 The difference is what you use for the timestamp but I don't think that should make any difference. Give it a try though.

ajbraus commented 10 years ago

Still not working. hmmm

  <% timestamp = (Time.now.to_f * 1000).round %>
  <% channel = @chat_room.channel_path %>
  <% signature = Digest::SHA1.hexdigest([ENV['PRIVATE_PUB_SECRET'], "#{channel}", timestamp].join) %>

  PrivatePub.sign({ 
  "server":"http://app_name.herokuapp.com/faye/faye",
  "timestamp":<%= timestamp %>,
  "channel": "<%= channel %>",
  "signature": "<%= signature %>"
  });
gregmolnar commented 10 years ago

Shouldn't the server URL be just "server":"http://app_name.herokuapp.com/faye/" ?

ajbraus commented 10 years ago

The second faye was to solve a different error

development:
  server: "//app_name.herokuapp.com/faye/faye"
  secret_token: <%= ENV["PRIVATE_PUB_SECRET"] %>
  signature_expiration: 3600 # one hour

I made a mistake disclosing the app name, could you edit your comment to be "app_name.herokuapp.com"

thanks!

gregmolnar commented 10 years ago

I removed it. I have no idea why it doesn't work for you. Next week I will try to set some time aside and debug it.

ajbraus commented 10 years ago

I definitely appreciate it. The goal is really to be able to be looking at a resource (a blog post for example) and then "strike up" a chat about it. So the user creates a message which creates a parent chat_room. Maybe my architecture is just wrong. Should I try to start a conversation without a pre-exisiting chat_room?

gregmolnar commented 10 years ago

You could create a channel for each blog post in your example and when the and subscribe the user to the channel when he/she opens the page. But what if they don't want to chat about that particular topic? :)

ajbraus commented 10 years ago

Exactly. If they don't want to strike up a conversation, I'll have these orphaned chat rooms. Also I only have 1-on-1 chats, so I'd have to make a billion chats. I'd rather just strike up the conversation and at the same time subscribe to all future messages.

gregmolnar commented 10 years ago

It should work with that workflow just there has to be a mistake in your setup or a bug in private_pub.

ajbraus commented 10 years ago

Maybe there should be a way to access the subscribe_to method in create.js?

gregmolnar commented 10 years ago

There should be a lot of new feature but Ryan is vanished. I may fork the project and start to support it.

ajbraus commented 10 years ago

Yeah where did Ryan go? I think private_pub is a huge attribute for rails, so it would be great to support it.

~Adam Braus 608-770-0230

On Feb 18, 2014, at 1:14 PM, Greg Molnar notifications@github.com wrote:

There should be a lot of new feature but Ryan is vanished. I may fork the project and start to support it.

— Reply to this email directly or view it on GitHub.

gregmolnar commented 10 years ago

I dunno where is he at. The last announcement on Railscast is from September 2013. He said he need some time off.

ajbraus commented 10 years ago

Any word on putting the subscribe_to helper into the create.js template? Thx!

ajbraus commented 10 years ago

Gregmolnar - I figured out a solution to this where i don't have to put the subscribe_to inside of the create.js template.

ajbraus commented 10 years ago

Just create the chat_room first before creating the first message

gregmolnar commented 10 years ago

I am glad you resolved it!

RavWar commented 9 years ago

Had the same issue, got it working with this: <%= raw strip_tags subscribe_to('/messages') %>