Removing events#

Sometimes you might need to remove a registered event. This can be done in two ways.

Removing all callbacks of a specific event name#

This removes all the functions that have been registered to the event name.

def callback_1(connection, data):
    print(f"Callback 1 {data}")

def callback_2(connection, data):
    print(f"Callback 2 {data}")
    # Remove all the callbacks of the event "test"
    server.remove_all_listeners("test")

server.on("test", callback_1)
server.on("test", callback_2)

When the callback_2() is called, it will remove all the callbacks of the event “test”. So when the socket receives the event “test”, nothing will be triggered.

Removing a specific callback#

This removes a specific callback from the event name.

def callback_1(connection, data):
    print(f"Callback 1 {data}")

def callback_2(connection, data):
    print(f"Callback 2 {data}")
    # Remove all the callbacks of the event "test"
    server.remove_listener("test", callback_1)

server.on("test", callback_1)
server.on("test", callback_2)

When the callback_2() is called, it will remove the callback callback_1() of the event “test”. So when the socket receives the event “test”, only the callback_2() will be triggered.

Important

These two examples are implemented in the server but you can also use them in the client.

Learn more#