Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

agnos.is Forums

  1. Home
  2. Tech
  3. how to make sockets in plugin working

how to make sockets in plugin working

Scheduled Pinned Locked Moved Tech
4 Posts 2 Posters 12 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    Guest
    wrote on last edited by
    #1

    Good day. I'm learning how to use sockets work in plugins

    'use strict';
    
    const pluginSockets = require.main.require('./src/socket.io/plugins'); 
    
    module.exports = function(params) {
      // Add custom function to the sockets object
      pluginSockets.customFunc = function(socket, data, done) {
        const response = 'Hello from server';
        done(null, response);
      };
    };
    

    then I open Developer Tools in Firefox and enter

    var script = document.createElement('script');
    script.src = '/socket.io/socket.io.js';
    document.head.appendChild(script);
    
    script.onload = function() {
      console.log('Socket.IO library loaded successfully');
    
      // Connect to the socket
      var socket = io.connect();
    
      // Emit the custom event and handle the response
      var dataToFetch = {}; // Replace with actual data if needed
      socket.emit('plugins.customFunc', dataToFetch, function(err, data) {
        if (err) {
          console.error('My socket customFunc', err, data);
        } else {
          console.log('Do stuff with this:', data); // Display the response
          alert(data); // Optionally show an alert
        }
      });
    };
    
    script.onerror = function() {
      console.error('Failed to load Socket.IO library');
    };
    

    This doesn't work. Could someone tell me how to fix it?
    Thank you. I'm new to Node.js and Nodebb.

    1 Reply Last reply
    0
    • baris@community.nodebb.orgB This user is from outside of this forum
      baris@community.nodebb.orgB This user is from outside of this forum
      [email protected]
      wrote on last edited by
      #2

      You can just use the below on the server side and it should work.

      'use strict';
       
      const pluginSockets = require.main.require('./src/socket.io/plugins'); 
       
      pluginSockets.customFunc = async function(socket, data) {
         const response = 'Hello from server';
         return response;
      };
      
      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        Guest
        wrote on last edited by
        #3

        Could someone explain me the difference between

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

        and

        2)var websockets = module.parent.require('./socket.io');
        
        1 Reply Last reply
        0
        • baris@community.nodebb.orgB This user is from outside of this forum
          baris@community.nodebb.orgB This user is from outside of this forum
          [email protected]
          wrote on last edited by
          #4

          First one is the suggested way of requiring core files, require.main always references the root of the nodebb folder so you don't have to worry about the require path when moving it around. Meanwhile module.parent references the parent module that required the module so you might have to update the path if you move it into a subfolder for example.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups