Saturday, August 7, 2010

Faye TCP/IP Traffic

‹prev | My Chain | next›

Up today, I would like to do a little exploration of faye channel communication. I would like to believe that, if my client has subscribed to channel "/foo" but not channel "/bar", then messages published to "/bar" are not seen (and ignored) by my client. I would like to cut down on traffic in high usage systems and am also interested in the ability to hold private conversations in faye.

To try this out, I establish a very simple faye server on my londo server. The code, taken directly off the faye site, is:
var Faye   = require('faye'),
server = new Faye.NodeAdapter({mount: '/faye'});

server.listen(8000);
I run the server with:
cstrom@londo:~/tmp/faye-test$ node app.js
Next, on my laptop, whitefall, I setup a simple faye client on the node command line:
node> var faye = require('faye');
node> var client = new faye.Client('http://londo:8000/faye');
node> client.subscribe('/foo', function(message) {puts("[foo]: " + message)});
Lastly, I establish a second client, back on the server, to publish messages:
node> var faye = require('faye');
node> var client = new faye.Client('http://londo:8000/faye');
node> client.publish("/foo", "test");
Checking back on my laptop, I do see that message in the node repl:
node> [foo]: test
[foo]: test
...
When I publish a message on the "/bar" channel, I see nothing in the client node repl. This is all as expected. If I have not subscribed to a channel, I should not see messages on it. But, are the messages being sent out and ignored in the client or is the server smart enough not to send out the messages?

I suppose that I could read the documentation to figure this out, but I prefer to actually see it in action. For that, I trust tcpdump. Here, I ask to see the packet contents in ascii (-X) and all of the packets, not just the first 96 bytes (-s 0):
cstrom@whitefall:~/repos/my_fab_game$ sudo tcpdump -i eth1 -X -s 0 host londo and not port 22
Now, when I send a message on the "/foo" channel, the channel to which I am currently subscribed on this laptop, I see the data packets coming back:
22:19:56.459204 IP londo.8000 > whitefall.local.47662: Flags [P.], seq 1563677724:1563678119, ack 4040761713, win 54, options [nop,nop,TS val 348053887 ecr 151564770], length 395
...
0x0030: 0908 b1e2 4854 5450 2f31 2e31 2032 3030 ....HTTP/1.1.200
0x0040: 204f 4b0d 0a43 6f6e 7465 6e74 2d54 7970 .OK..Content-Typ
0x0050: 653a 2061 7070 6c69 6361 7469 6f6e 2f6a e:.application/j
0x0060: 736f 6e0d 0a43 6f6e 6e65 6374 696f 6e3a son..Connection:
...
0x00f0: 3a22 2f6d 6574 612f 636f 6e6e 6563 7422 :"/meta/connect"
0x0100: 2c22 7375 6363 6573 7366 756c 223a 7472 ,"successful":tr
0x0110: 7565 2c22 6164 7669 6365 223a 7b22 7265 ue,"advice":{"re
0x0120: 636f 6e6e 6563 7422 3a22 7265 7472 7922 connect":"retry"
0x0130: 2c22 696e 7465 7276 616c 223a 302c 2274 ,"interval":0,"t
0x0140: 696d 656f 7574 223a 3630 3030 307d 7d2c imeout":60000}},
0x0150: 7b22 6368 616e 6e65 6c22 3a22 2f66 6f6f {"channel":"/foo
0x0160: 222c 2264 6174 6122 3a22 7465 7374 2030 ","data":"test.0
0x0170: 3122 2c22 636c 6965 6e74 4964 223a 2270 1","clientId":"p
0x0180: 6e79 396d 3931 306f 3665 3268 326c 7832 ny9m910o6e2h2lx2
...
It is pretty cool to see this in action. Aside from that, it is pretty much as I expected—I am subscribed to the "/foo" channel, I already know that messages are getting back here, so I ought to see the TCP/IP data.

But what happens when I publish to the "/bar" channel to which I am not subscribed on this laptop? Nothing. No TCP/IP data is transmitted back. Perhaps I should have expected that (especially if I had read the documentation), but to actually see it... nice.

Before calling it a night, I do one last sanity check. I load a faye client in Firefox 3, which does not support websockets. I have no reason to expect the client and server to behave any differently, but I have to try it out in order to believe it. In the browser, I subscribe to the "/baz" channel:
>>> var client = new Faye.Client('http://londo:8000/faye');
>>> client.subscribe("/baz", function(message){console.debug(message)});
Again, when I publish to the "/bar" channel, there is no TCP/IP activity. When I publish to the "/baz" channel, I do see TCP/IP traffic. Nice.

About ready to call it a night, it occurs to me, what happens if I subscribe to a channel "/*"? In that case, I do see messages published to all channels:
node> client.subscribe('/*', function(message) {puts("[*]: " + message)});
...

node> [*]: bar test 01
[*]: foo test 01
So it seems that it is relatively easy to snoop on all faye messages. Tomorrow, I will investigate the extensions facility in faye to see if it is possible to limit that ability.


Day #188

No comments:

Post a Comment