From 404fb4e03e5a9670144e0c5b4924b9aa4e622a70 Mon Sep 17 00:00:00 2001
From: Gergely Lekli <gl90010@930940.no-reply.drupal.org>
Date: Mon, 25 Jan 2016 08:21:24 -0800
Subject: [PATCH] Issue #2656330: Enhance token channel handling.

---
 nodejs.js     | 13 +++++++++++++
 nodejs.module | 14 ++++++++++----
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/nodejs.js b/nodejs.js
index 19a6200..b49af72 100644
--- a/nodejs.js
+++ b/nodejs.js
@@ -121,6 +121,19 @@ Drupal.Nodejs.checkConnection = function () {
   }
 };
 
+Drupal.Nodejs.joinTokenChannel = function (channel, contentToken) {
+  var joinMessage = {
+    channel: channel,
+    contentToken: contentToken
+  };
+
+  // Add to the global settings so that the user is joined again when the client reconnects.
+  drupalSettings.nodejs.contentTokens = drupalSettings.nodejs.contentTokens || {};
+  drupalSettings.nodejs.contentTokens[channel] = contentToken;
+
+  Drupal.Nodejs.socket.emit('join-token-channel', joinMessage);
+};
+
 Drupal.Nodejs.sendAuthMessage = function () {
   var authMessage = {
     authToken: drupalSettings.nodejs.authToken,
diff --git a/nodejs.module b/nodejs.module
index 9879523..ba1a7d0 100644
--- a/nodejs.module
+++ b/nodejs.module
@@ -32,14 +32,20 @@ function nodejs_send_content_channel_message($message) {
 /**
  * Send a content channel token to Node.js.
  *
- * @param mixed $channel
- * @param mixed $notify_on_disconnect
- */
-function nodejs_send_content_channel_token($channel, $notify_on_disconnect = FALSE) {
+ * @param $channel
+ *   The channel to generate the token for.
+ * @param $notify_on_disconnect
+ *   If TRUE, other clients will be notified when this client disconnects.
+ * @param $user_data
+ *   Arbitrary custom data that can be stored with the token. This will be
+ *   broadcast to other clients when the user connects.
+ */
+function nodejs_send_content_channel_token($channel, $notify_on_disconnect = FALSE, $user_data = FALSE) {
   $message = (object) array(
     'token' => nodejs_generate_content_token(),
     'channel' => $channel,
     'notifyOnDisconnect' => $notify_on_disconnect,
+    'userData' => $user_data,
   );
 
   // Http request went ok, process Node.js server response.
-- 
2.3.8 (Apple Git-58)

