question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing method of non-WebSocketApp object as callback does not receive the WebSocketApp object as an argument

See original GitHub issue

Previous versions of websocket-client allowed passing methods of non-WebSocketApp objects as callbacks and have them receive the WebSocketApp object as the first argument. This was very convenient.

Recent versions (0.53, 0.54) introduced a change to _callback in _app.py that checks if the callback is a method. And if so, not pass the WebSocketApp object to the callback. This is fine if the callback method is bound to a WebSocketApp subclass, but very annoying if not.

Passing bound methods of other classes as callbacks is a very common pattern and it would be great if websocket-client could re-enable the original behavior for such cases.

A fix would be very simple, just one line of code:

    def _callback(self, callback, *args):
        if callback:
            try:
                if inspect.ismethod(callback) and isinstance(callback.__self__, type(self)):
                    callback(*args)
                else:
                    callback(self, *args)

This change would allow 0.53/0.54 behavior for methods of WebSocketApp subclasses and 0.52 behavior for methods of objects that are not WebSocketApp subclasses.

As an aside, the older behavior seems more common in python modules with callbacks. I can understand the desire for the 0.53 change, but I think the more typical pattern is to have the subclass methods get called directly (i.e., have on_open, on_message, etc. methods in WebSocketApp that don’t do anything and are overridden in subclasses) if no callback attribute is specified. But that is, of course, up to the author.

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
ajgbarnescommented, Mar 21, 2019

I got around this issue for now using a lambda function so, for example:

...
ws = WebSocketApp(url, 
   on_open=lambda ws, msg: app.ws_open(ws), 
   on_message=lambda ws, msg: app.ws_message (ws, msg),
   on_error=lambda ws, msg: app.ws_error(ws, msg),
   on_close=lambda ws: app.ws_close(ws))
...

I hope this helps someone

7reactions
shdsjdcommented, Nov 28, 2018

@pbechliv just use 0.52.0. the changes starts from 0.53.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing additional arguments to a callback object that only ...
Looking at the source for the WebSocketApp it says that it has 2 arguments, 1) the WebSocketApp class itself and 2) the utf-8...
Read more >
websocket/_app.py — websocket-client 1.4.2 documentation
on_message (function) – Callback object which is called when received data. on_message has 2 arguments. The 1st argument is this class object.
Read more >
Add a WebSocket Route to your Flask 2.x Application
Introducing the Flask-Sock Extension ... The ws object that is passed to the route is the actual WebSocket connection, which the function can...
Read more >
tornado.websocket — Bidirectional communication to the ...
The origin argument is the value of the Origin HTTP header, the url responsible for initiating this request. This method is not called...
Read more >
Creating a Python WebSocket client for AWS AppSync real ...
Since the GraphQL spec doesn't dictate how subscriptions work, ... event callbacks (similar to JS sockets); Creates a WebSocketApp object, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found