Trying to use the "action:connected" hook
-
Attempting to use the "action:connected" hook but can't seem to get this simple plugin to execute. I'm guessing I've done something wrong in my plugin.json or the code itself.
plugin.json
"id": "nodebb-plugin-clienttest", "name": "NodeBB Client Test", "description": "NodeBB Plugin", "url": "http://www.osnapgames.com", "library": "./library.js", "hooks": [ { "hook": "action:connected", "method": "connected" } ] }
library.js
var plugin = {}; plugin.connected = function() { console.log("CONNECTED"); }; module.exports = plugin;
-
Hey @overlawled --
action:connected
is a client-side hook and is fired when a connection is established to the websocket server by a user, but thehooks
section ofplugin.json
is for server-side hooks only.If you want to run something on the server-side on NodeBB start, listen for the
static:app.load
hook on the server side.Unless you wanted to do something on the client-side when a user connects, in which case that's different...
-
That's exactly what I'm looking for, do I use the action:connected hook for that? How do I register my plugin with the client?
-
Here's a sample:
https://github.com/NodeBB/nodebb-plugin-quickstart/blob/master/plugin.json#L19-L22
Then use jquery to subscribe to this hook:
$(window).on('static:app.load', function() {});
In case you haven't found this link yet, here's a list of all the hooks (automatically updated everytime we push a commit to NodeBB master):
https://github.com/NodeBB/NodeBB/wiki/Hooks -
Quickstart plugin helps a lot, thanks!
Looking through its code I've now got another question.
socket.emit('admin.reload');
It is possible to emit a custom signal from the client side upon "action:connected" and have the server listening for it?
-
Yup. There's many examples of it being used in various plugins, here's one:
Server:
https://github.com/psychobunny/nodebb-plugin-question-and-answer/blob/master/library.js#L97-L119 -
Perfect, thanks!
-
Sorry, another question.
Once the data hits the server I want to be able to call login. Logging into the forums needs to be invisible to the user and is tied into our existing login system. Is this a possibility? What function do I need to call on the server to begin the login process, with the data sent via the socket.
-
Sounds to me like you're better off doing a post request? Like: https://github.com/NodeBB/NodeBB/blob/master/public/src/client/login.js
-
Rather than send the required data via socket?
-
If you want to use sockets, then tap into this function:
https://github.com/NodeBB/NodeBB/blob/master/src/routes/authentication.js#L82
For the
req
object, pass inip
as a parameter. This is so NodeBB can log attempts. It would probably look something similar to this:SocketPlugins.myplugin.login = function(socket, data, callback) { auth.login({ip: socket.request.socket.remoteAddress}, data.username, data.password, callback); });
-
Seems to ignore the login override I have in place. Perhaps POST is better. To use POST I need CSRF included but no combination of require, define, etc with ['csrf'] as an argument is loading csrf.
require(['csrf'], function(csrf) { console.log(csrf); }
If anyone has a better suggestion I'm all ears. Looking for deep integration into our site/system without needing to login multiple times. Login will happen on our accounts page/main site. If someone visits the forums I want to grab their info (id/authkey) stored in a cookie and pass it onto the forums to verify their login without any user interaction. I'd like to use as much of the existing framework in nodebb as possible and simply plugin to our existing API to verify the info. I've managed to do the last part no problem using action:auth.overrideLogin.
-
require(['csrf'], function(csrf) { console.log(csrf.get()); });
This works for me, the code you pasted above is missing the ending
);
-
Thanks @psychobunny!
-
Yeah np, hope you manage to figure it out. Will post your site on our social network after its all done. Is your designer doing a custom theme and stuff for Quasar?
-
Likely going to take an existing one and modify it for now. Hitting Greenlight on Thursday with a show in Mississauga this weekend.
-
That's in my area, PM me details maybe I'll come swing by