Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Calling functions on socket message (

Calling functions on socket message (


When I start a new instance of GameServer, it sets up the Socket and listeners as follows.

var GameServer = function() {
    this.player = new Player();
    var that = this;

    // Setup sockets and listeners
    var socket = io.listen(8000);
    socket.sockets.on('connection', function(client) {
        client.on('message', that.onSocketMessage);
        client.on('disconnect', that.onSocketDisconnect);

I then have two prototypes GameServer.prototype.onSocketMessage & onSocketDisconnect.

I have two problems with the current code:

  1. Using that = this and the closure? Function. Looks ugly.

  2. When onSocketMessage is called, the idea is it works out what the message is then calls another function within GameServer. Only this isn't possible as now this belongs to the system. See below:


function onSocketMessage() {

this.player is no longer available as this. is no longer part of GameServer.

Should my socket setup and message passing be handled outside of GameServer function and prototypes?

Or how else could I resolve this?



Ok so I have tried this and it works but looks pretty ugly I think:

var socket = io.listen(8000);
socket.sockets.on('connection', function(client) {
    client.on('message', function (data) {
        that.onSocketMessage(this, that, data);
    client.on('disconnect', function() {
        that.onSocketDisconnect(this, that);

Can it be improved upon?

Problem courtesy of: Chris Evans


Two things that may help. Thing the first:

You can modify a function's vision of this using the bind method.

socket.sockets.on('connection', function(client) {
    client.on('message', this.onSocketMessage);
    client.on('disconnect', this.onSocketDisconnect);

Notice the call to bind(this) at the end of the function; this instructs JavaScript to create a closure for you, making whatever this is outside the function, this inside the function. (If you wanted to make this inside the function, say, MySomething, you could just as easily call bind(MySomething), though bind(this) is the most common use).

Thing the second:

You can store data in a Socket.IO socket. So, for example, if one socket is always associated with a player, you can do

socket.set('player', player, function() {
  // callback is called when variable is successfully stored
  console.log('player has been stored');

// and later

socket.get('player', function(err, player) {
  // callback is called either with an error set or the value you requested

The get and set methods take callbacks because the Socket.IO data store can be set to something other than an in-memory store; for example, Redis.

Solution courtesy of: Michelle Tilley


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Calling functions on socket message (


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription