How to hook websocketsjswebsocketsinitiosocketson


  • I'm writing a simple plugin to add a restart nodebb button to admin panel. I need to add an socket.on('command') command to io.sockets.on to check when I click a button and execute some js server side. Can I hook to add a new command or is there another way of going about this?

  • GNU/Linux Admin

    We wanted this, but there's actually no way to programmatically restart a node instance.

    You can't assume people are using forever or supervisor either, so the best you can do is force node to exit.


  • @julian So I have a plugin that adds a button to admin panel. How would I go about making it tell node to exit? Or for that matter execute any server side code when clicked?

  • GNU/Linux Admin

    process.exit(), in a pinch...


  • @julian I'm asking how to call that from a client side script? My idea is to somehow add a socket.on event that I would execute from client via socket.emit. I'm not sure if this is best method but it is what I seen done with other buttons.


  • Without being able to figure out how to properly run server side code via clicking a client side button from plugin system I am just going to code it directly in nodebb and skip plugin system. I was just thinking it would be nice to share it but without help I am unable to figure this out.

  • GNU/Linux Admin

    Hi @cani

    Yes, the above method would be how I would have the client initiate a server command.

    It's pretty dangerous to expose that functionality, which is why we don't offer the ability to restart NodeBBs from the admin console.


  • @julian

    I still don't get how to call that specific command from client script. As I expected adding it to the client side script gives the following error;

    Uncaught ReferenceError: process is not defined
    

    This is obviously because process is not client side, it is server side.

    I am asking how to execute a specific server function from client javascript. How would I go about getting the server to recognize I clicked the button and act accordingly.

    I really am not sure how to explain this any more direct. Not understanding how to properly communicate with server functions from client without modifying internal non plugin related files will prevent me from making my modifications plugins accessible to other people.

    I am past working on a restart plugin, I have coded it directly into nodebb to fit my needs. I am now asking in general how to accomplish these tasks from within the plugin system.

    Thank you, please don't take my frustration negatively. I love NodeBB!

  • Admin NodeBB

    Client side you do this

    socket.emit('yourEventName',{data:1},function(){console.log('server responded');});
    
    

    Server side you do this :

    socket.on('yourEventName', function(data, callback) {
        console.log('client said', data);
        // call your server side JS function here.
        callback();
    });
    

    The formatting is messed up dunno why @julian?

    The emit will send an event that the server will catch and execute the code, you can call the callback to let the client that the server is done. We usually put the socket code on the server side in websockets.js. Client side you can use the global socket object anywhere.


  • @baris

    I understand that and have accomplished it via doing it server side in websockets.js . My problem occurs when trying to do it from a plugin. My plugin looks as such and errors on the socket.on line;

    var	fs = require('fs'),
    	path = require('path'),
    	canIrestart = 
    		init: function() {
    			/*socket.on("api:testrestart", function testrestart() {
    				winston.info('[testrestart] Testing socket.on');
    			});*/
    		},
    		admin: {
    			menu: function(custom_header, callback) {
    				custom_header.plugins.push({
    					"route": '/plugins/canIrestart',
    					"icon": 'icon-edit',
    					"name": 'canIrestart'
    				});
    
    				return custom_header;
    			},
    			route: function(custom_routes, callback) {
    				fs.readFile(path.join(__dirname, 'public/templates/admin.tpl'), function(err, tpl) {
    					custom_routes.routes.push({
    						route: '/plugins/canIrestart',
    						method: "get",
    						options: function(req, res, callback) {
    							callback({
    								req: req,
    								res: res,
    								route: '/plugins/canIrestart',
    								name: canIrestart,
    								content: tpl 
    							});
    						}
    					});
    
    					callback(null, custom_routes);
    				});
    			},
    			activate: function(id) {
    
    			}
    		}
    		socket.on('yourEventName', function(data, callback) {
    			console.log('client said', data);
    			callback();
    		}
    
    	};
    
    
    
    canIrestart.init();
    module.exports = canIrestart;
    
  • Admin NodeBB

    Yeah that won't work right now because you don't have access to the socket object in the plugin. websockets.js doesn't expose the socket objects for the users.

    We are considering adding a hook in websockets.js inside the connection handler. Something like :

    plugins.fireHook('event:connect', {socket: socket});
    

    Then plugins can listen to that and use the socket object passed in to attach event handlers like you do in your plugin.

  • GNU/Linux Admin

    This post is deleted!

Suggested Topics

| |