0

Whenever a new user connects to socket.io an event handler will create a new class for that user called Events

const eventHandler = socket => {
  new Events(socket);
};

and Events constructor will set its socket to the socket its been given upon creation

class Events {
  constructor(socket) {
    this.socket = socket;
    this.socket.on('test', this.test)
  }

  test() {
    console.log(this.socket); // logs undefined
  }
}

the issue is why this.socket returns undefined? I think I'm missing how something in socket.io works, help is appreciated

typicallearner
  • 238
  • 1
  • 13

1 Answers1

0

I got my own answer, It actually has nothing to do with socket.io but rather how this works, further reading: How to access the correct `this` inside a callback?

typicallearner
  • 238
  • 1
  • 13