websockets icon indicating copy to clipboard operation
websockets copied to clipboard

Object oriented Websocket Client and Server example

Open pravindamodaran opened this issue 6 years ago • 7 comments

I'm trying to implement object oriented Websocket client and server. Since websockets is completely coroutine based, I'm finding it difficult to go ahead with implementation. Is it possible to wrap it around classes? Are there any examples of using it in this way?

pravindamodaran avatar Dec 16 '19 06:12 pravindamodaran

websockets is actually quite object-oriented, so I'm not sure what you're asking for. Of course you can wrap it in a class but it really depends on what you want to do.

class Wrapper:
    @staticmethod
    async def connect(*args):
        ws = await websockets.connect(*args)
        return Wrapper(ws)

    def __init__(self, ws):
        self.ws = ws

Having the class instance before the connection is also possible of course. But then you'd need a connect method and it would introduce a lot of illegal states for your potential other methods (where you need a running connection). Something I'd always recommend to avoid.

lgrahl avatar Dec 16 '19 10:12 lgrahl

The man obviously want a more detailed example than what's provided in the documentation. If you've never used asyncio and you want to do something a little bit more complex than a "Hello world", you won't get far.

Francwa avatar Dec 17 '19 11:12 Francwa

What's missing from my example?

lgrahl avatar Dec 17 '19 11:12 lgrahl

The man obviously want a more detailed example than what's provided in the documentation. If you've never used asyncio and you want to do something a little bit more complex than a "Hello world", you won't get far.

Yeah, you're right. I've just started with asyncio and wanted to leverage an object oriented approach to websockets using asyncio. Currently we are using zmq sockets (pyzmq library) and since its synchronous, its easy in this case.

Basically I have a parent class Endpoint and 2 child classes - Client and Server. Endpoint will common methods, while the child classes will have specific ones.

Wanted to try out similarly for websockets, but stuck due to my inexperience in using asyncio.

websockets is actually quite object-oriented, so I'm not sure what you're asking for. Of course you can wrap it in a class but it really depends on what you want to do.

class Wrapper:
    @staticmethod
    async def connect(*args):
        ws = await websockets.connect(*args)
        return Wrapper(ws)

    def __init__(self, ws):
        self.ws = ws

Having the class instance before the connection is also possible of course. But then you'd need a connect method and it would introduce a lot of illegal states for your potential other methods (where you need a running connection). Something I'd always recommend to avoid.

Yes I needed something like this. Is it possible to add such an example (very simple one would suffice though) in the doc? Or is it out of scope of this project?

pravindamodaran avatar Dec 17 '19 12:12 pravindamodaran

I'm going to flag this as a documentation issue for now, but it's unclear to me what a documentation would look like. If you'd like to work on a patch, let's talk before you put too much time into it.

aaugustin avatar Dec 25 '19 15:12 aaugustin

I'm still unclear on the scope of this issue. I'd rather avoid including too much asyncio tutorial in the websockets docs. Closing for now but willing to consider a concrete proposal in this area.

aaugustin avatar Jul 26 '20 20:07 aaugustin

Found this myself. Would be great to have an OO example

toby-j avatar Apr 29 '25 17:04 toby-j