Skip to content

ratquaza/modular-socket.io

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

modular-socket.io

The modular-socket.io package can be used to load events for a socket.io WebSocket from a given directory.

Usage example:

const io = require("socket.io");

const path = require("path");
const fs = require("fs");

const mod = require("modular-socket.io");
const publicEvents = mod(path.join(__dirname, "api", "events"));
const adminEvents = mod(path.join(__dirname, "api", "admin", "events"));

...

io.on("connect", (socket) => {
    publicEvents(socket);
    if (/* authorized */) {
        adminEvents(socket);
    }
});

Documentation

require("modular-socket.io")

Returns the createEvents function

createEvents(directory)

Attempts to load all .js files within the given directory.

Returns a function that sets the events up for passed in sockets using the files within the given directory.

Files must export a function by default, that can take as many arguments as you wish. The first argument will always be the socket that emitted the event.

About

Handle socket.io 'on' events through module files in a given directory.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published