Wednesday, August 11, 2010

Troubleshooting Faye Channels

‹prev | My Chain | next›

After taking some time to explore faye in some depth, I am back to working through my (fab) game today.

When last I was working on it, I had ripped out a lot of custom written comet code in favor of faye pub-sub with no apparent loss in functionality. Taking the game for a quick spin tonight, I notice immediately that there is a problem: new players in the game are not told about already existing players. I specifically wrote a faye channel for handling this situation, so I ought to start there for investigation.

Instead, I head back to the node.js REPL to see if some of the skills that I accumulated might be of some help. I know that I can subscribe to all faye channels in node-repl using a wildcard subscription. Since messages can be of any type, I iterate over the properties to see what I can see:
node> var wc = client.subscribe('/**', function(message) {
for (var prop in message) {
puts('['+prop+']' + message[prop]);
When I enter the room as player "foo", this is what I see in the node-repl:
node> [0]a
Dang. That is not going to be quite as much help as I had hoped.

There are two messages represented in that output. The first is the string "all" (represented as an array of three characters here). This is the request on the "/players/query" channel for the list of all players. It should elicit a response listing those players, but that response is conspicuously missing. The other message is the announcement of the new player "foo" (along with current x-y coordinates) to all previously play game members.

Also missing from that output is any information regarding the channel on which the message was sent. Since I am subscribed to all channels, that would definitely help. I have to admit that I was surprised that the channel information was not an attribute on the message. Surprised and even a little disappointed... Until I remembered that this is pub-sub and that faye does it right™. Faye is no more, no less than a message transport. That is all I get when subscribing to a channel—the message.

Ah well, good to be reminded of that. I will file that away (and hopefully remember it). But I am still left with wondering what is going on with my currently-playing query. That turns out to be a relatively simple solution—once I look at the actual code. When I converted my player list from a global array to an object, I left a reference to the global players array in place:
    this.faye.subscribe("/players/query", function(q) {
var ret = [];
for (var id in players) {
self.faye.publish("/players/all", ret);
Easy enough, I replace the global with the attribute (_) that holds the list of players on the new object:
    this.faye.subscribe("/players/query", function(q) {
var ret = [];
for (var id in self._) {
self.faye.publish("/players/all", ret);
That is a fine stopping point for tonight. Up tomorrow, I need to make sure that the idle timeout / quit player functionality is still working.

Day #192

No comments:

Post a Comment