Monday, August 2, 2010

A Slight Pause Before Faye Connect

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:

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: