Thursday 29 November 2018

Passing socket into a module

I've got a Socket.IO / NodeJS application I'm working on that has a module for handling various buttons (just to keep things clean and separate). If I make handleButtonScripts use io.emit, everything works fine, but I want to be able to notify just that one socket that pressed the button (hence client.emit) but I never get the HelloWorld emit on button presses. It's like the client var isn't passing through to the module the way I'm intending. Can anyone shed some light here for me?​Thanks in advance!​buttons.jsvar handleButtonScripts = require('../helpers/handleButtonScripts'); module.exports = function(io) { io.on('connection', function(client) { client.on('buttonClicked', (button) => { handleButtonScripts(io, client, button) }); }); }; handleButtonScripts.jsfunction handleButtonScripts(io, client, button) { client.emit('HelloWorld'); } ​

Submitted November 29, 2018 at 08:38PM by Sintex

No comments:

Post a Comment