Publishing messages to external clients

38 views
Skip to first unread message

bjhaid

unread,
Jul 26, 2011, 10:53:22 AM7/26/11
to Faye users
I have gone through this thread:
http://groups.google.com/group/faye-users/browse_thread/thread/372137bff34b974c/96faa9238faa7133?lnk=gst&q=server-side+ruby#

It seems similar to what I want to achieve, but I want to make post
available to clients not subscribing to rails server, but I want to
update them when there is an update to my rails db.

I really am not sure how to handle this since they would not be
pointing to any url for rails, they are subscribing to the channel I
created, but I have challenges getting the server to publish the
required messages to them.

Thanks

James Coglan

unread,
Jul 26, 2011, 3:17:33 PM7/26/11
to faye-...@googlegroups.com

On Jul 26, 2011 3:53 PM, "bjhaid" <abejide...@gmail.com> wrote:
> I really am not sure how to handle this since they would not be
> pointing to any url for rails, they are subscribing to the channel I
> created, but I have challenges getting the server to publish the
> required messages to them.

So, you have two servers, a Rails app and a Faye server? I would use an ActiveRecord observer to send updates to Faye using Net::HTTP. I'd write an example only I'm sending this from my phone.

bjhaid

unread,
Jul 26, 2011, 6:04:23 PM7/26/11
to Faye users
I think I have fixed this, I added some stuff to my new action in my
controller and it works, I dont mind seeing your code too, probably
would be a better way

On Jul 26, 8:17 pm, James Coglan <jcog...@gmail.com> wrote:

bjhaid

unread,
Jul 27, 2011, 8:33:35 AM7/27/11
to Faye users
Hi, this code worked yesterday, but when i woke up this morning,
whenever my rails server hits the new action it freezes, any reason
why, and James, I would appreciate your codes again.

Thanks
17 def new
18 @incoming = Incoming.create({:exten_number =>
params[:exten_number], :cid_number => params[:cid_number]})
19
20 if @incoming.update_attributes(params[:incoming])
21
22 faye = Faye::Client.new('http://0.0.0.0:9292/faye')
23 EM.run {
24 faye.publish("/incomings", 'text' => 'Update html view!')
25 }
26 end
27 respond_to do |format|
28 format.html # index.html.erb
29 format.xml { render :xml => @incoming }
30 end
31 end
32

James Coglan

unread,
Jul 27, 2011, 6:50:23 PM7/27/11
to faye-...@googlegroups.com

On Jul 27, 2011 1:33 PM, "bjhaid" <abejide...@gmail.com> wrote:
>
>  23 EM.run {
>  24 faye.publish("/incomings", 'text' => 'Update html view!')
>  25 }

This is because EM.run starts an infinite loop and never exits until something calls EM.stop. I would just publish like this (I should do a better job of documenting this):

msg = JSON.dump('channel' => '/incomings', 'data' => {'text' => 'Update html view!'})
uri = URI.parse('http://0.0.0.0:9292/faye')
Net::HTTP.post_form(uri, :message => msg)

bjhaid

unread,
Jul 29, 2011, 4:21:23 AM7/29/11
to Faye users
Thanks for the response, i actually booted rails from thin server and
the previous code worked, i would use your approach should i encounter
any more error.

Thanks once again

On Jul 27, 10:50 pm, James Coglan <jcog...@gmail.com> wrote:

bjhaid

unread,
Jul 29, 2011, 5:52:14 AM7/29/11
to Faye users
i know this should not be a question for this group but I have had to
battle this all day, wondering what is wrong, i am trying to write a
jquery to check if the element returned from the broadcast belongs to
a client that is identified by an element in his DOM, the codes are
pasted @ https://gist.github.com/1113498, but it just doesnt work and
i dont know why.

Thanks

James Coglan

unread,
Aug 14, 2011, 5:08:18 PM8/14/11
to faye-...@googlegroups.com
On 29 July 2011 10:52, bjhaid <abejide...@gmail.com> wrote:
i know this should not be a question for this group but I have had to
battle this all day, wondering what is wrong, i am trying to write a
jquery to check if the element returned from the broadcast belongs to
a client that is identified by an element in his DOM, the codes are
pasted @ https://gist.github.com/1113498, but it just doesnt work and
i dont know why.

This is a guess but I'm guessing these lines are to blame:

      var a = (data.cid_number);
      var b = (data.caller_name);
      var c = new String($('#agentExten').text());

      if (a == c) {

a can never be equal to c because c is a String object rather than a primitive string. (The semantics of == are also really weird and the operator is best avoided.) JavaScript kinda messes up the integration between these and in practise nobody ever uses new String() to make strings.

This should be fine for you, since $.text() just returns a string there's no need to wrap a 'new String()' around it. I'm also using === rather than == since it's much more predicatable -- == will try to coerce the types of its arguments while === does not.

      var a = (data.cid_number);
      var b = (data.caller_name);
      var c = $('#agentExten').text();

      if (a === c) { 
Reply all
Reply to author
Forward
0 new messages