Monday, August 2, 2010

A Slight Pause Before Faye Connect

‹prev | My Chain | next›

today, I try to solve the mystery left to me last night. When starting up the fab.js backend in my (fab) game, I was having trouble connecting to the faye server. Actually connecting to the server from browsers was working fine, but trying to connect from within the server was not.

I subscribe to a faye channel in the server so that I can keep a running tally of the players in the game. That tally is used to tell new players in the room where the existing player are. Since that subscription is not working, the players in the game are not stored.

This would be a pain to troubleshoot if I had not previously added a resource to query player statuses. When things are working, I can get a list of players in the room via curl:
cstrom@whitefall:~/repos/my_fab_game$ curl -i http://localhost:4011/status
HTTP/1.1 200 OK
Connection: keep-alive
Transfer-Encoding: chunked

fred
timeout?:[object Timer]
idle from:Mon Aug 02 2010 18:58:07 GMT-0400 (EDT)
x 271 y 309
Last night, accessing this resource got me an empty response:
cstrom@whitefall:~/repos/my_fab_game$ curl -i http://localhost:4011/status
HTTP/1.1 200 OK
Connection: keep-alive
Transfer-Encoding: chunked

I was able to get this working by establishing a timeout before listening:
setTimeout(function(){
var client = new faye.Client('http://localhost:4011/faye');
client.subscribe("/move", function(message) {
update_player_status(message);
});
}, 1000);
But why should I need that timeout to make it work?

The answer to that is I do not need the timeout. After fiddling with it for a bit (a little over a minute or so), I realized that the subscription suddenly started working. After acking through the faye code a bit, I find that the connection timeout defaults to 60 seconds. There is also retry code that will set a timeout and retry a failed connection upon completion of the timeout.

What I believe is happening is that, when the code for my (fab) game is first read/executed by the node.js interpreter, the faye server has not quite had a chance to establish itself when the faye client tries to connect to it. The faye client then goes into wait-and-see mode. When it sees that the server is in place, voilà, the faye client connects and all is well.

Why is the faye server not running yet? That I am not quite sure about. The fab/faye server is defined earlier in the source code than the faye client. I would expect that the code would block until the faye server spins up and then move onto the faye client definition. My best guess, and I will leave it as a guess for now, is that the faye server has spun up, but that the TCP/IP port is not quite ready.

Regardless, the solution to the problem remains the same as last night: set a timeout before trying to subscribe the faye client and all is well. I could also have attempted to decrease the timeout:
   var client = new Faye.Client('http://localhost:8000/faye', {
timeout: 1
});
But that feels rather hamfisted. I am happy with a timeout. At least for now.

Day #183

No comments:

Post a Comment