Handling socket event sent from theme (on client) inside plugin (on server)

NodeBB Plugins
  • I have created a custom theme which allows users to interact with topics in more than default ways, essentially it is rating system for topics. I have successfully added a new hash field to topics on "topic.save".

    My theme has extra buttons for custom rating, clicking on which through my theme.js I am sending socket emit to be handled on server side. But my plugin doesn't seems to be responding to those socket events. However, if I put same socket.on handler inside src/socket.io/index.js, it works.

    I need to find a way to handle custom socket events emitted from my theme through my plugin. Otherwise I don't want to tangle with the core source of the software, which of course is not a wise decision.

  • It's not recommended to add listeners to any other socket.io namespace besides plugins.js, since that one is purposefully empty and built for plugins to use.

    If you add a new method there, e.g.:

    var SocketPlugins = module.parent.require('./socket.io/plugins');
    SocketPlugins.myPlugin = {};
    SocketPlugins.myPlugin.myMethod = function(someObj) { ... };

    Then that method will be invoked when you do this on the client side:

    socket.emit('plugins.myPlugin.myMethod', someObj, callback);

  • @julian great will try this now, thanks for quick reply!

Suggested Topics

  • 0 Votes
    4 Posts
  • 1 Votes
    1 Posts
  • 0 Votes
    6 Posts
  • 2 Votes
    13 Posts
  • 8 Votes
    18 Posts