I'm developping a livetracking module, I would like to be able to access publishMessageToContentChannel directly from my nodejs extension without having to go trought sendMessageToBackend.

My goal is just to speed up the publishing process of the tracker device to the users. Passing trought the backend is really bad for performance.

What I request is changing :

var extensionsConfig = {
  'publishMessageToChannel': publishMessageToChannel,
  'publishMessageToClient': publishMessageToClient,
  'addClientToChannel': addClientToChannel,
  'settings': settings,
  'channels': channels,
  'io': io,
  'tokenChannels': tokenChannels,
  'authenticatedClients': authenticatedClients,
  'request': request,
  'sendMessageToBackend': sendMessageToBackend
};

with

var extensionsConfig = {
  'publishMessageToChannel': publishMessageToChannel,
  'publishMessageToClient': publishMessageToClient,
  'addClientToChannel': addClientToChannel,
  'settings': settings,
  'channels': channels,
  'io': io,
  'tokenChannels': tokenChannels,
  'authenticatedClients': authenticatedClients,
  'request': request,
  'sendMessageToBackend': sendMessageToBackend
  'publishMessageToContentChannel': publishMessageToContentChannel
};

I guess there might be some safety reasons why this function is not included by default into the extensionsConfig object but I can't see any at the moment.

Can I have some explanations on this topic please ?
++

Comments

Anonymous’s picture

Version: 7.x-1.6 » 7.x-1.x-dev
Status: Active » Fixed

there's no good reason. initially, it was this way because when i wrote server.js i had NFI what i was doing with node.js code, and the extension stuff was added later, and is pretty clunky.

so, here ya go:

http://drupalcode.org/project/nodejs.git/commit/c45e63939bca75335ad335d4...

julien66’s picture

That's perfect.
=> Thank again Beejeebus !

julien66’s picture

Status: Fixed » Needs work

And now I feel stupid... !
Cleaning the livetracking module after having a working version I was trying to use this publishMessageToContentChannel function directly from node... And it eventually result in an error since it's built to handle a request from the backend ! (And not directly from Nodejs) :

var publishMessageToContentChannel = function (request, response) {
  var sentCount = 0, requestBody = '';
  request.setEncoding('utf8');
  request.on('data', function (chunk) {
    requestBody += chunk;
  });
  request.on('end', function () {
    try {
      var message = JSON.parse(requestBody);
      if (settings.debug) {
        console.log('publishMessageToContentChannel: message', message);
      }
    }
    catch (exception) {
      console.log('publishMessageToContentChannel: Invalid JSON "' + requestBody + '"', exception);
      response.send({error: 'Invalid JSON, error: ' + exception.toString()});
      return;
    }

I can't see any simple way of using this function directly.
I guess it involves a new way of writting the active nodejs functions for both 'direct' and 'from-backend' use. I'm not clever enough to find a clean way of doing it.
Beejeebus, any hint on this ?
I believe this is the last request to provide a clean livetracking module.
Thanks a thrillion !
++

rho_’s picture

I've worked around this by modifying and including the function in my server extenstion. You'll need to pass the config variable along with your message, but seems to work alright.

function sendMessageToTokenChannel(message, config) {
  if (!message.hasOwnProperty('channel')) {
    console.log('publishMessageToContentChannel: An invalid message object was provided.');
    return;
  }
  if (!config.tokenChannels.hasOwnProperty(message.channel)) {
    console.log('publishMessageToContentChannel: The channel "' + message.channel + '" doesn\'t exist.');
    return;
  }

  for (var socketId in config.tokenChannels[message.channel].sockets) {
    config.publishMessageToClient(socketId, message);
  }
}

I imagine something like this could be implemented in server.js and passed to the extensionsConfig object.

julien66’s picture

Status: Needs work » Closed (works as designed)

You rock rho_ !
I didn't realised the config object actually contained everything I needed to rebuild the function in my own extension. I commited the change to my project (marked commit as authored by you !) :
=> https://drupal.org/node/2035675/commits

@Beejeebus, please forgot about the previous patch.
I close the issue, it works as designed !

  • Commit c45e639 on 7.x-1.x, 8.x-1.x, 8.x-1.x-head by beejeebus:
    #2036461: allow server extensions direct access to...