Naming convention for socket.io message?


  • Anime Lovers

    Is there any standard naming convention for socket.io message?

    I did some search and find out some patterns below:

    1. nodebb plugin - event:{type} eg. event:user_status_change
    2. custom plugin - {pluginName}:{type} eg. nodebb-plugin-myplugin:data_change

    Any official comment on this? Thanks!


  • Global Moderator

    @Bruce-Lee something like

    var PluginSockets = require.main.require('./src/socket.io/plugins');
    PluginSockets.yourPlugin = {};
    PluginSockets.yourPlugin.whatever = function (a, b, c) {};
    

    On the server side equates to

    socket.emit('plugins.yourPlugin.whatever', a, b, c);
    

    On the client.


  • Admin

    Actually it's

    //server side
    PluginSockets.yourPlugin.whatever = function (socket, data, callback) {
    //socket.uid - uid of the user making the call or 0 for guests
    //data - data passed from the client
    //callback - notify client when you are done, pass err as first param
    };
    
    //client side
    socket.emit('plugins.yourPlugin.whatever', {foo: 'bar'}, function(err, data) {
         if (err) {
             app.alertError(err.message);
         }
         //success
    });
    

  • Anime Lovers

    Thanks @baris and @PitaJ for the nice info, I will try it out.


Log in to reply
 


Star

Looks like your connection to NodeBB was lost, please wait while we try to reconnect.