I have various clients' sockets connect to my server and I need to define an event on each of them, but based on some logic, not unconditionally. Like:
for(i= 0; i<= this.users.length; i++) {
if (i=== actionUserOrdinal) {
io.sockets.socket(mySocketId).on('action', function(action) {...
but doing so it gets defined multiple times for some sockets during the course of running the app. And so, it gets invoked multiple times too (by the same trigger).
And if I define it the default way,
io.sockets.on('connection', function(socket) {
socket.on('action', function(data) {
...
I cannot access my main app logic's variables and such. Unless I make some things global.
One solution I thought of was to delete the event after it is triggered
for(i= 0; i<= this.users.length; i++) {
if (i=== actionUserOrdinal) {
thisocket = io.sockets.socket(mySocketId);
io.sockets.socket(mySocketId).on('action', function(action) {
delete thisocket._events.action;
(Thanks @ArdiVaba)
But I discover this is flaky and the event still gets fired twice sometimes.
Is there some other way to define the event such that it doesn't gets define more than once, by either defining it in my main app's logic itself, or by defining it in its default scope yet I still be able to access my main app's variables without going global?