1

I am trying to create a live chat app using flask socket-io. But where I use connect event it shows this "Method must have a first parameter, usually called 'self' " error. How can I fix this. I am using flask-socketio 4.3.0. Here is my code.

    @socketio.on('disconnect')
    def disconnect():
        print('Client disconnected')

    @socketio.on('connect')
    def connect():
        print("Client connected")

here is screen shot.

enter image description here

kartoos khan
  • 409
  • 6
  • 22
  • Because flask is written in Python, and [Python instance methods need self](https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self) –  Jun 27 '20 at 15:05
  • adding self never executes this line print('Client disconnected') when user connects or disconnects – kartoos khan Jun 27 '20 at 15:39
  • 1
    But that is why your IDE is complaining. I would read up on the Flask documentation as to why its methods don't need a `self`. –  Jun 27 '20 at 15:41
  • Is this just something the IDE is pointing out, or something Python complains about? – 89f3a1c Jun 28 '20 at 04:37

1 Answers1

1

You are showing an incomplete code example in your question. You are defining these handlers inside a class, not as standalone functions, correct?

The handlers are supposed to be regular functions. If you want to keep them in your class, try making them static methods:

    @socketio.on('disconnect')
    @staticmethod
    def disconnect():
        print('Client disconnected')

    @socketio.on('connect')
    @staticmethod
    def connect():
        print("Client connected")
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152