jmhobbs

Impromptu logging from a socket.io connection

I recently participated in a live streamed event that provided a "watching now" counter usin socket.io. Basically it was a super simple node.js script which incremented or decremented a variable when users joined and left the channel, and broadcasted the count to it's subscribers. What I didn't realize until right before the event that we might want to have a record for users on page at a given point in the broadcast. With so little time before the broadcast, I didn't want to tinker with the server and break it, so I did the next best thing, I logged from the subscriber side.

I put up a quick PHP script on my laptop that allowed cross-domain access from the origin server and logged the incoming counter.

Then, in Chrome's JavaScript console, I just hooked updates from socket.io into an XHR to provide the values to my PHP.

socket.on('update', function ( data ) { $.get('http://localhost/logger.php', { count: data.count } ); } );

It worked like a charm, I didn't have to mess with the server at a crucial point, and we got the data we needed.