Hello, I am starting implementing a chat with faye but I am experiencing some problem implementing the "presence" system.
What I am doing is the following:
When a user starts a new faye client, he also publishes a message on the channel /presence/connect. Then I monitor that event with the following code
faye_server.bind(:publish) do |client_id, channel, data|
if channel == '/presence/connect' FayeUtils.client_connected :user_id => data['user_id'], :client_id => client_id FayeUtils.update_contacts_list end
end
where FayeUtils.client_connected handles all the logic. FayeUtils.update_contacts_list tries to publish a new message to all faye clients.
The missing part is the update of the online contacts list inside the browsers. If I try to publish data via a server client inside FayeUtils.update_contacts_list, faye hangs. It doesn't look like an infinite loop caused by recursion, but more like if something goes in timeout.
Any idea on how to inspect that or is there any better way to implement that?
On 9 December 2011 00:17, slampis <stefanolam...@gmail.com> wrote:
> faye_server.bind(:publish) do |client_id, channel, data|
> if channel == '/presence/connect' > FayeUtils.client_connected :user_id => data['user_id'], :client_id > => client_id > FayeUtils.update_contacts_list > end
> end
> where FayeUtils.client_connected handles all the logic. > FayeUtils.update_contacts_list tries to publish a new message to all > faye clients.
Can you publish all the code involved in this, or is that not possible? That would make it easier to debug. What I assume your code is doing is:
* client_connected() adds the user_id to a set and probably maps the user_id to its current client_id * update_contacts_list() does something like, client.publish('/present', 'user_ids' => all_the_user_ids)
What type of client are you using to do this? Do you know how it's connected? Is it connected via WebSocket/HTTP or is the in-process faye_server.get_client connection?
> > where FayeUtils.client_connected handles all the logic. > > FayeUtils.update_contacts_list tries to publish a new message to all > > faye clients.
> Can you publish all the code involved in this, or is that not possible? > That would make it easier to debug. What I assume your code is doing is:
> * client_connected() adds the user_id to a set and probably maps the > user_id to its current client_id > * update_contacts_list() does something like, client.publish('/present', > 'user_ids' => all_the_user_ids)
Your assumptions are right, if it helps I will post my code as I get home. For now, I can tell you that update_contacts_list does not use client.publish but makes a direct HTTP POST to faye with restclient. The restclient call is correct, I know because I use it in other contexts.
> What type of client are you using to do this? Do you know how it's > connected? Is it connected via WebSocket/HTTP or is the in-process > faye_server.get_client connection?
By client you mean server client? If yes, any client: as I said I make a simple HTTP request. I don't understand the second question. How can I obtain that information?
On 13 December 2011 07:48, Stefano Lampis <stefanolam...@gmail.com> wrote:
> For now, I can tell you that update_contacts_list does not use > client.publish but makes a direct HTTP POST to faye with restclient. > The restclient call is correct, I know because I use it in other > contexts.
This is just a guess, but it sounds like what might be happening is that you're making a blocking call to Faye (a single-threaded, event-driven server) while it's in the middle of trying to respond to another request. Try either using `faye_server.get_client.publish(...)` to publish your data, or use an evented HTTP client like em-http-request, or the new faye-websocket library.
As you can see my code is based on the basic example. Can you confirm that this approach starts a single threaded server?
I've given a quick look to faye-websocket. I understand that it would be easy to implement a simple server, but my question is? Can I still use the faye javascript client? If yes, do I lost compatibility with browsers which are not supporting web sockets?
On Dec 13, 8:59 am, James Coglan <jcog...@gmail.com> wrote:
> On 13 December 2011 07:48, Stefano Lampis <stefanolam...@gmail.com> wrote:
> > For now, I can tell you that update_contacts_list does not use > > client.publish but makes a direct HTTP POST to faye with restclient. > > The restclient call is correct, I know because I use it in other > > contexts.
> This is just a guess, but it sounds like what might be happening is that > you're making a blocking call to Faye (a single-threaded, event-driven > server) while it's in the middle of trying to respond to another request. > Try either using `faye_server.get_client.publish(...)` to publish your > data, or use an evented HTTP client like em-http-request, or the new > faye-websocket library.
> As you can see my code is based on the basic example. > Can you confirm that this approach starts a single threaded server?
You need to use Thin to serve Faye, since it uses async responses. To do this, run
rackup -s thin -E production faye.ru
> I've given a quick look to faye-websocket. I understand that it would > be easy to implement a simple server, but my question is? > Can I still use the faye javascript client? > If yes, do I lost compatibility with browsers which are not supporting > web sockets?
faye-websocket is *only* a WebSocket implementation. It's not a shim, an abstraction, or a higher-level messaging service. It's just the WebSocket-handling code I've extracted from Faye. If you start a faye-websocket server, you can connect to it using the WebSocket/MozWebSocket interfaces in browsers.
> > As you can see my code is based on the basic example. > > Can you confirm that this approach starts a single threaded server?
> You need to use Thin to serve Faye, since it uses async responses. To do > this, run
> rackup -s thin -E production faye.ru
> > I've given a quick look to faye-websocket. I understand that it would > > be easy to implement a simple server, but my question is? > > Can I still use the faye javascript client? > > If yes, do I lost compatibility with browsers which are not supporting > > web sockets?
> faye-websocket is *only* a WebSocket implementation. It's not a shim, an > abstraction, or a higher-level messaging service. It's just the > WebSocket-handling code I've extracted from Faye. If you start a > faye-websocket server, you can connect to it using the > WebSocket/MozWebSocket interfaces in browsers.
On 14 December 2011 10:23, Stefano Lampis <stefanolam...@gmail.com> wrote:
> I've checked out my scripts and this is the exact command I use to > start faye
> rackup faye.ru -D -s thin -E production -p 9292
> but still, it hangs under the conditions I have described.
I can confirm that making a blocking call blocks Thin: run this:
require 'rack' require 'uri' require 'net/http'
app = lambda do |env| body = case env['PATH_INFO'] when '/hi' uri = URI.parse('http://localhost:8000/') Net::HTTP.get_response(uri).body else 'Hello!' end
[200, {'Content-Type' => 'text/plain'}, [body]] end
If you request http://localhost:8000/hi, the server hangs. This is because you deadlock the server by blocking the event loop with an HTTP call that can never return, because the server it's calling has its event loop blocked. If that makes sense.
Use the Faye client to publish messages, or an async http/websocket client, and you should be fine.
> app = lambda do |env| > body = case env['PATH_INFO'] > when '/hi' > uri = URI.parse('http://localhost:8000/') > Net::HTTP.get_response(uri).body > else > 'Hello!' > end
> [200, {'Content-Type' => 'text/plain'}, [body]] > end
> If you requesthttp://localhost:8000/hi, the server hangs. This is because > you deadlock the server by blocking the event loop with an HTTP call that > can never return, because the server it's calling has its event loop > blocked. If that makes sense.
> Use the Faye client to publish messages, or an async http/websocket client, > and you should be fine.
Can you provide an example for use Faye client in server events? I need to use a client in server subscribe event but I'm not sure of the right way to achieve this.
Thank you
On Dec 14, 1:44 pm, Stefano Lampis <stefanolam...@gmail.com> wrote:
> > If you requesthttp://localhost:8000/hi, the server hangs. This is because > > you deadlock the server by blocking the event loop with an HTTP call that > > can never return, because the server it's calling has its event loop > > blocked. If that makes sense.
> > Use the Faye client to publish messages, or an async http/websocket client, > > and you should be fine.
On 19 December 2011 10:11, Jerome B <blanchard.jer...@gmail.com> wrote:
> Can you provide an example for use Faye client in server events? > I need to use a client in server subscribe event but I'm not sure of > the right way to achieve this.
Can you clarify where exactly you're trying to use the client, and maybe some example code? Is this inside your application code, or in a Faye extension, or a Faye event listener? Are you on Ruby or Node?
> Can you provide an example for use Faye client in server events? > I need to use a client in server subscribe event but I'm not sure of > the right way to achieve this.
> Thank you
> On Dec 14, 1:44 pm, Stefano Lampis <stefanolam...@gmail.com> wrote: > > Using the faye client worked like a charm. > > Many thanks for your help
> > On Dec 14, 1:26 pm, James Coglan <jcog...@gmail.com> wrote:
> > > On 14 December 2011 10:23, Stefano Lampis <stefanolam...@gmail.com> > wrote:
> > > > I've checked out my scripts and this is the exact command I use to > > > > start faye
> > > If you requesthttp://localhost:8000/hi, the server hangs. This is > because > > > you deadlock the server by blocking the event loop with an HTTP call > that > > > can never return, because the server it's calling has its event loop > > > blocked. If that makes sense.
> > > Use the Faye client to publish messages, or an async http/websocket > client, > > > and you should be fine.
> On Mon, Dec 19, 2011 at 11:11 AM, Jerome B <blanchard.jer...@gmail.com>wrote:
> > Hello,
> > Can you provide an example for use Faye client in server events? > > I need to use a client in server subscribe event but I'm not sure of > > the right way to achieve this.
> > Thank you
> > On Dec 14, 1:44 pm, Stefano Lampis <stefanolam...@gmail.com> wrote: > > > Using the faye client worked like a charm. > > > Many thanks for your help
> > > On Dec 14, 1:26 pm, James Coglan <jcog...@gmail.com> wrote:
> > > > On 14 December 2011 10:23, Stefano Lampis <stefanolam...@gmail.com> > > wrote:
> > > > > I've checked out my scripts and this is the exact command I use to > > > > > start faye
> > > > If you requesthttp://localhost:8000/hi, the server hangs. This is > > because > > > > you deadlock the server by blocking the event loop with an HTTP call > > that > > > > can never return, because the server it's calling has its event loop > > > > blocked. If that makes sense.
> > > > Use the Faye client to publish messages, or an async http/websocket > > client, > > > > and you should be fine.