Node Socket.io Socket.on() Across Multiple Files
Solution 1:
There are different ways to structure things depending upon how your code is structured and how you might want to reuse or structure your modules. Here are some of the choices:
Pass
ioto every module's constructor that wants to listen for incoming socket.io events and let every module that wants to listen for a message, use thatiovalue to create its ownio.on('connection', socket => { socket.on('someMsg', localHandler)} );.Export
iofrom your main socket.io module and then have every module that wants to listen to an incoming messageimport/require()the main socket.io module (to get theiovalue) and then use thatiovalue to create its ownio.on('connection', socket => { socket.on('someMsg', localHandler)} );.Have your main socket.io module export a function for registering a listener for an incoming socket.io message and then just have one
io.on('connection', ...)handler in your main socket.io module that listens for all the events that were requested.
Post a Comment for "Node Socket.io Socket.on() Across Multiple Files"