Node Roundup: NodObjC, SockJS, French Node Blog
You can send your node modules and articles in for review through our contact form or @dailyjs.
NodObjC
NodObjC (npm: NodObjC) by Nathan Rajlich is an Objective-C bridge:
It uses the
BridgeSupportfiles to dynamically generate an API from an Objective-C “Framework”, and uses node-ffi to dynamically interact with the Objective-C runtime.
If you’re an Objective-C developer, then you should be able to follow this basic app structure:
var $ = require('NodObjC')
// First you need to "import" the Framework
$.import('Foundation')
// Setup the recommended NSAutoreleasePool instance
var pool = $.NSAutoreleasePool('alloc')('init')
// NSStrings and JavaScript Strings are distinct objects, you must create an
// NSString from a JS String when an Objective-C class method requires one.
var string = $.NSString('stringWithUTF8String', 'Hello Objective-C World!')
// Print out the contents (calling [string description])
console.log(string)
// → Prints "Hello Objective-C World!"
pool('drain')
Sending messages to objects looks like this: obj('func', arg). So a call like [array insertObject:obj atIndex:5]; would be written as array('insertObject', obj, 'atIndex', 5).
This means Apple’s native APIs are accessible from Node, so you could create GUI apps for Mac OS. There are examples out there of GUI code already: cocoa-hello-world2.js.
SockJS
SockJS (License: MIT) is a WebSocket emulation library that attempts to provide simple APIs for both server and clients, while remaining as close to the WebSocket API as possible. It’s designed to work from behind restrictive corporate proxies, and browsers that don’t support WebSocket. The project includes the server-side library, sockjs-node and client-side: sockjs-client.
Using sockets in the browser looks like this:
var sockjs = new SockJS('http://mydomain.com/my_prefix');
sockjs.onopen = function() {
console.log('open', e.data);
};
sockjs.onmessage = function(e) {
console.log('message', e.data);
};
sockjs.onclose = function(e) {
console.log('close', e.data);
};
To me this looks like the current draft of the WebSocket spec (Editor’s Draft 10 September 2011):
var socket = new WebSocket('ws://game.example.com:12010/updates');
socket.onopen = function() {
setInterval(function() {
if (socket.bufferedAmount == 0)
socket.send(getUpdateData());
}, 50);
};
This is different to Socket.IO:
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
There’s a detailed article about the library on RabbitMQ’s blog: SockJS – WebSocket emulation and it includes details on how SockJS can be used with load balancing.
One interesting detail I noticed in the project README is it falls over to polling rather than Flash:
No Flash inside (no need to open port 843 – which doesn’t work through proxies, no need to host ‘crossdomain.xml’, no need to wait for 3 seconds in order to detect problems)
French Node Blog
Vincent Rabah emailed us to share his French Node blog: IT Wars: Node articles. He’s got articles covering Socket.IO, Twitter, Vim, Windows, and a variety of other topics including log monitoring.
