You are here

function nodejs_send_content_channel_token in Node.js integration 7

Same name and namespace in other branches
  1. 8 nodejs.module \nodejs_send_content_channel_token()
  2. 6 nodejs.module \nodejs_send_content_channel_token()

Send a content channel token to Node.js.

Parameters

mixed $channel:

mixed $notify_on_disconnect:

1 call to nodejs_send_content_channel_token()
nodejs_watchdog_form_dblog_filter_form_alter in nodejs_watchdog/nodejs_watchdog.module
Implements hook_form_FORM_ID_alter().

File

./nodejs.module, line 42

Code

function nodejs_send_content_channel_token($channel, $notify_on_disconnect = FALSE) {
  $message = (object) array(
    'token' => nodejs_generate_content_token(),
    'channel' => $channel,
    'notifyOnDisconnect' => $notify_on_disconnect,
  );

  // Http request went ok, process Node.js server response.
  if ($node_response = Nodejs::sendContentToken($message)) {
    if ($node_response->status == 'success') {

      // We always set this in drupal.settings, even though Ajax requests will not
      // see it. It's a bit ugly, but it means that setting the tokens for full
      // page requests will just work.
      drupal_add_js(array(
        'nodejs' => array(
          'contentTokens' => array(
            $channel => $message->token,
          ),
        ),
      ), array(
        'type' => 'setting',
      ));
      $node_response->token = $message->token;
      return $node_response;
    }
    else {
      watchdog('nodejs', 'Error sending content channel token for channel "%channel". Node.js server response: %error', array(
        '%channel' => $channel,
        '%error' => $node_response->error,
      ));
      return FALSE;
    }
  }
  else {
    return FALSE;
  }
}