Correct usage of events in NodeJs - Concerning "this" context

I am designing a communication server in Node that handles incoming messages (sent by client1) and transfers them to someone else (client2), who answers the message and sends the answer back, via the server, to client1. The communication happens via WebSockets, which implies an open connection from each client to the server.

Thus I implemented a ConnectionManager to which I can register any new connections when a new client comes online. Every connection gets assigned a messageQueue in which all incoming messages are cached before processing.

At the end of processing, I have a ServerTaskManager, who generates Output-Tasks for the server, telling him a message to send and a receiver to receive it.

This ServerTaskManager emits a Node-Event (inherits from EventEmitter) upon registering a new serverTask to which the server listens.

Now I would like my ConnectionManager to also listen to the event of the serverTaskManager, in order to make him push the next message in the messageQueue into processing.

Now the problem is, that I can catch the ServerTaskManager event within the ConnectionManager just fine, but, of course, the "this" within the listener is the ServerTaskManager, not the ConnectionManager. Thus calling any "this.someFunction()" functions that belong to the ConnectionManager won't work.

Here is some code:

/**
* ServerTaskManager - Constructor
* Implements Singleton pattern. 
*/
function ServerTaskManager() 
{
var __instance;

ServerTaskManager = function ServerTaskManager()
{
    return __instance;
}

ServerTaskManager.prototype = this;

__instance = new ServerTaskManager();
__instance.constructor = ServerTaskManager;

return __instance;
}

util.inherits(ServerTaskManager, EventEmitter);

/**
* ConnectionManager - Constructor
* Also implements Singleton pattern. 
*/
function ConnectionManager() 
{
var __instance;

ConnectionManager = function ConnectionManager()
{
    return __instance;
}

ConnectionManager.prototype = this;

__instance = new ConnectionManager();
__instance.constructor = ConnectionManager;
__instance.currentConnections = [];

// Listen for new serverInstructions on the serverTaskManager
serverTaskManager.on('newInstruction', function(messageObject, currentReceiver)
{
    this.processNextMessage(currentReceiver);
});

return __instance;
}

util.inherits(ConnectionManager, EventEmitter);

Now when I run this and the "newInstructions" event is triggered by the serverTaskManager, node throws:

 TypeError: Object #<ServerTaskManager> has no method 'processNextMessage'

Which is of course true. The function I want to call belongs to the ConnectionManager:

 /**
  * Starts processing the next message
  * 
  * @param connectionId (int) - The ID of the connection, of which to process the next message.
  */
 ConnectionManager.prototype.processNextMessage = function (connectionId)
 {
     // Some Code...
 }

So obviously, when listening to the ServerTaskManager event, "this" within the listener is the ServerTaskManager. Now how do I call my ConnectionManager's function from within the listener?

I hope I am not completely misled by how events and listeners and/or prototypical extensions work (in Node). This project is by far the most advanced that I have worked on in JavaScript. Normally I am only coding PHP with a little bit of client side JS.

Thx in advance for any hints! Worp

Like this.

serverTaskManager.on('newInstruction', function(messageObject, currentReceiver)
{
     ConnectionManager.processNextMessage(currentReceiver);
});

Or like this.

serverTaskManager.on('newInstruction', function(messageObject, currentReceiver)
{
     ConnectionManager().processNextMessage(currentReceiver);
});

PS: your question is unnecessarily long. When posting code, don't necessarily post your example. It is much easier to boil your code down to the simplest form that exhibits the behavior you are seeing. You'll get more quality responses this way.