One example of receiving messages for node.js. I was using nodejs-websocket module as WebSocket server. 

My server looks like this:

var ws = require("nodejs-websocket")

var server = ws.createServer(function (conn) {
    console.log("New connection")
    conn.on("close", function (code, reason) {
        console.log("Connection closed")
    })

}).listen(8081)

setInterval(function () {
	console.log("Sending 'Test'");
	try {
		server.connections.forEach(function (con) {
				console.log("Sending 'Test'");
				con.sendText("Test");
		});
	} catch (e) {
		console.log('Error!');
		console.log(e);
	}
}, 1000);

Client looks like this:

window.onload = function () {

	function MyConnectionWrapper() {
		var self = this,
			createSocket = function () { return new WebSocket('ws://127.0.0.1:8081') },
			ws = createSocket();
			
			self.onopen = function (myFunc) {
				ws.onopen = myFunc;
			}
			
			self.onmessage = function (myFunc) {
				console.log('In self: ' + myFunc);
				ws.onmessage = myFunc;
			};
	}
	
	myConn = new MyConnectionWrapper();

	myConn.onmessage(function (myFunc) {
		console.log('In myConn: ' + myFunc);
	});
}

Here notice lines:

myConn = new MyConnectionWrapper();

myConn.onmessage(function (myFunc) {
  console.log('In myConn: ' + myFunc);
});

With first line:

myConn = new MyConnectionWrapper();

we have created socket, because automatically following code is executed:

createSocket = function () { return new WebSocket('ws://127.0.0.1:8081') },

with that myConn became WebSocket, and with self we assigned onmessage method to myConn:

self.onmessage = function (myFunc) {
  console.log('In self: ' + myFunc);
  ws.onmessage = myFunc;
};

In this case myFunc will be function which we assigned like this:

myConn.onmessage(function (myFunc) {
 console.log('In myConn: ' + myFunc);
});

Example download from here.