• Hey!

    I want to use WebSockets for my own plugin, and I found this:

    It doesn't work for me.

    `2020-12-23T19:35:15.502Z [4567/3108] - error: Error: Cannot find module './socket.io/modules'``

    I also tried

    const ownsockets = require.main.require('./socket.io/modules')
    

    and

    const ownsockets = require.main.require('./socket.io')
    

    Nothing works.

    Is a custom websocket still possible or is it outdated?
    Tried to import it in libary.js

  • Global Moderator

    @pasib you need to do

    const pluginSockets = require.main.require('./src/socket.io/plugins');
    

    See this for an example: https://github.com/pitaj/nodebb-plugin-calendar/blob/1.0.x/src/lib/sockets.js


  • @pitaj Thank you for your fast response.

    Could you give mit a litte example how to emit and receive messages? I'm new to javascript and also new to nodebb. It is hard for me to follow the programming language especially in plugin dev.

    I have this in socket.io documentation

    socket.on('chat message', (msg) => {
        console.log('message: ' + msg);
      });
    

    something comeplete different .... 😕 than

    pluginSockets.calendar = {};
    pluginSockets.calendar.canPostEvent = async ({ uid }, { pid, tid, cid, isMain }) => {
      const neither = {
        canPost: false,
        canPostMandatory: false,
      };
    
      if (!isMain && await getSetting('mainPostOnly')) {
        return neither;
      }
    
      if (!(pid || tid || cid)) {
        return neither;
      }
    
      let promises;
    
      if (pid) {
        promises = [
          can.posts(privilegeNames.canPost, pid, uid),
          can.posts(privilegeNames.canMandatoryPost, pid, uid),
        ];
      }
      if (tid) {
        promises = [
          can.topics(privilegeNames.canPost, tid, uid),
          can.topics(privilegeNames.canMandatoryPost, tid, uid),
        ];
      }
      if (cid) {
        promises = [
          can.categories(privilegeNames.canPost, cid, uid),
          can.categories(privilegeNames.canMandatoryPost, cid, uid),
        ];
      }
    
      const [canPost, canPostMandatory] = await Promise.all(promises);
      return {
        canPost,
        canPostMandatory,
      };
    };
    

    Could you give me a little introduction? I'm sorry - I am a noob 🙉 but I really want to learn....


    I got it

    1. Import custom Sockets via
    const chatSockets = require.main.require('./src/socket.io/plugins');
    
    1. Define a test socket
    chatSockets.sendMessage = function(socket, data, callback) {
    	console.log("Working?");
    	console.log(data);
    	callback(null, "It worked!");
    }
    
    1. Go into Browser and Emit an Event
    socket.emit('plugins.sendMessage', {data: "Some data"}, function(err, result) {
    	alert(result);
    });
    

    U can use this snipped in your main.js client script file:

    socket.emit('plugins.sendMessage', {data: "Some data"}, function(err, result) {
    			alert(result);
    	});
    

  • I still have a question. Everything works fine at the moment. Send data from the client to the server isn't a problem now.

    But how can I emit a event to all connected clients serverside?

    Something e.g

    myPlugin.emit('plugins.publishMessage', {data: "Some data"}, function(err, result) {
    			console.log(result);
    	});
    

    After one hour spending searching different topics and some code I found the solution.

    Besides the custom clientside Sockets

    const myPluginSockets = require.main.require('./src/socket.io/plugins');
    

    you have to define the Server Sockets too

    const myPluginSockets = require.main.require('./src/socket.io/plugins');
    const serverSockets = require.main.require('./src/socket.io');
    

    Now you can emit events from server side to the clients:

    serverSockets.server.sockets.emit('messageReceive', data);
    

    to receive the event clientside, you can use following in ur main.js file:

    socket.on('messageReceive', function(data){
          console.log(data);
    });
    

    Client console output:
    {msg: "my message"}


    Maybe I'll write a little Tutorial on this because I am probably not the only one who does not understand it so easily.

Suggested Topics

| |