Making nodejs Drupalchat work with Commons trusted contact 2/2

Posted by on Sep 23, 2014 in Commons, Drupal, Javascript, nodeJs, PHP | No Comments

After the first part of this post, we had a pretty good NodeJs chat, but there is still some issues (see the end of the part 1).

1 – Clean the user trusted contact.

The user trusted contact list that you get in the user object is not exactly perfect. In fact you get all the approuved and non approuved trusted contacts.

What I have done is pretty simple, I have juste created a function to clean this list and used it before sending it to the NodeJs server.

In my custom .module :

After this, you just need to call this function in the message callback after the user_load :

in the rse_nodejs_chat_trusted_channel_add() function :

We also need to use this function at the user connexion. To do that we have to alter another Drupalchat callback.

Finaly we user the clean trusted contact function in our custom callback.

2 – Update the user trusted contact.

Now we need to update the user trusted contact list so the user can see the new trusted contact without loging out or rebooting the nodeJs server.

To do this, I have modified the server.js file in the nodejs module. I usualy do not do that but here this is much more simple than rewriting a tons of code.

So in the server.js file just ask the server to update the user on each pages, event if his tokken is still valid.

We call the backend to update the current user using the line :

So we need to define the ‘updateClientCallback’ function :


 3 – Conclusion

There seems to be a lot of code to make nodeJs drupalchat works with trusted contacts, but the truth is that we just reuse the existing code and make some little change to send the informations we need to our nodeJs server. I am not entirely satisfied with the last part because I have directly modified the server.js file, but I do not see how to do this properly without rewriting a huge amount of code.

I hope this can be usefull to you, and if you have some improvement I would be happy to hear it.