On Github rh0 / drupalcampatx-slides
$subject = "To all users:"; $body = "Hello World!"; nodejs_broadcast_message($subject, $body);
function nodejs_broadcast_message($subject, $body) { $message = (object) array( 'broadcast' => TRUE, 'data' => (object) array( 'subject' => $subject, 'body' => $body, ), 'channel' => 'nodejs_notify', ); nodejs_enqueue_message($message); }nodejs.module
function messagejs_nodejs_user_channels($auth_user) { $channels = array(); if (user_access('receive messagejs notifications', $auth_user)) { $channels[] = 'messagejs_general'; } return $channels; }messagejs.module
function messagejs_send_message($body, $subject = '') { $notification = new stdClass(); $notification->channel = 'messagejs_general'; $notification->data['subject'] = $subject; $notification->data['body'] = $body; $notification->callback = 'nodejsNotify'; nodejs_enqueue_message($notification); }messagejs.module
function swapit_demo_nodejs_handlers_info() { return array( drupal_get_path('module', 'swapit_demo').'/swapit_demo.client.js', ); }swapit_demo.module
$message = new stdClass(); $message->channel = 'swapit_demo'; $message->data['body'] = $user_msg; $message->callback = 'swapIt'; nodejs_send_message($message);swapit_demo.module
Drupal.Nodejs.callbacks.swapIt = { //grab the message and inject into the DOM callback: function (message) { if(message.channel == 'swapit_demo') { $('form #nodejs-selector').html(message.data.body); } } };swapit_demo.client.js
exports.setup = function (config) { ... }
process.on('client-connection', function (sessionId) { console.log('... got connection event for session ' + sessionId); config.publishMessageToClient(sessionId, { data: { subject: 'Example extension', body: 'Hello, you just connected.' } } ); })
nodejs/nodejs.server.extension.js.example
var message = { type: 'nodeToDrupal', messageBody: 'Hello from the client side!' };
$("#node-to-drupal-form").submit(function() { Drupal.Nodejs.socket.emit('message', message); return false; });
node_to_drupal.client.js
exports.setup = function (config) { process.on('client-message', function (sessionId, message) { console.log('Got a message from the client. Take a look: '); console.log(message); // Do some stuff ...
config.publishMessageToClient(sessionId, { data: { subject: 'Success', body: "Huzzah!" } }); } };
node_to_drupal.server.extension.js
function node_to_drupal_nodejs_message_callback($type) { switch($type) { case 'nodeToDrupal': return array('node_to_drupal_handler'); } return false; }
function node_to_drupal_handler($message, &$responce) { $message_body = $message['messageBody']; watchdog('node_to_drupal', $message_body, array(), WATCHDOG_INFO); $responce = 'Message logged to Watchdog!'; }
node_to_drupal.module
message.messageType = 'nodeToDrupal';
config.sendMessageToBackend(message, function(error, responce, body) { if(error) { console.log('Error sending message to backend.', error); return; } console.log('Response from drupal: ', body); });
nodejs_send_content_channel_token('dchess_game_' . $node->nid);
nodejs_send_content_channel_message($message);
function sendMessageToTokenChannel(message, config) { if (!message.hasOwnProperty('channel')) { console.log('... invalid message object was provided.'); return; } if (!config.tokenChannels.hasOwnProperty(message.channel)) { console.log('... channel "' + message.channel + '" doesn\'t exist.'); return; } for (var socketId in config.tokenChannels[message.channel].sockets) { config.publishMessageToClient(socketId, message); } }dchess.server.js
Works great with pushing messagesto the client.
You can build more complex node applicaitons with this...
but should you?