'Difference between websocket and websockets

I don't understand the difference between websocket (websocket client) and websockets. I would like to understand the difference to know which one uses to be optimal.

I have a code that seems to do the same thing.

import websockets
import asyncio
import json

async def capture_data():
    uri = "wss://www.bitmex.com/realtime?subscribe=instrument:XBTUSD"
    #uri = "wss://www.bitmex.com/realtime"
    async with websockets.connect(uri) as websocket:
        while True:
            data = await websocket.recv()
            data = json.loads(data)
            try :
                #print('\n', data['data'][0]["lastPrice"])
                print('\n', data)
            except KeyError:
                continue


asyncio.get_event_loop().run_until_complete(capture_data())
import websocket
import json

def on_open(ws):
    print("opened")

    auth_data = {
        'op': 'subscribe',
        'args': ['instrument:XBTUSD']
    }
    ws.send(json.dumps(auth_data))

def on_message(ws, message):
    message = json.loads(message)
    print(message)

def on_close(ws):
    print("closed connection")


socket = "wss://www.bitmex.com/realtime"                 
ws = websocket.WebSocketApp(socket, 
                            on_open=on_open, 
                            on_message=on_message,
                            on_close=on_close)     
ws.run_forever()

EDIT:

Is Python powerful enough to receive a lot of data via websocket? In times of high traffic, I could have thousands of messages per second and even tens of thousands for short periods of time.

I've seen a lot of things about Nodejs that might be better than python but I don't know js and even if I just copy code from the internet I'm not sure I wouldn't waste time if I have to send the data to my python script and then process it.



Solution 1:[1]

websocket-client implements only client-side. If you want to create a WebSocket Server you have to use websockets.

websocket-client implements WebSocketApp which is more JavaScript like implementation of websocket.

I prefer WebSocketApp run as thread:

socket = "wss://www.bitmex.com/realtime"                 
ws = websocket.WebSocketApp(socket, 
                            on_open=on_open, 
                            on_message=on_message,
                            on_close=on_close) 

wst = threading.Thread(target=lambda: ws.run_forever())
wst.daemon = True
wst.start()

# your code continues here ...

Now you have asynchronous WebSocket. Your mainframe program is running and every time, when you receive the message, function on_message is called. on_message process the message automatically, without mainframe interruption.

Solution 2:[2]

websockets is a Python standard libary, currently suggested to use.

As far as I understand the main difference, from the client API programming, is that websocket-client support callbacks programming paradigm, in a way similar of javascript web client APIs.

Read the websockets documentation FAQs:

Are there onopen, onmessage, onerror, and onclose callbacks?

No, there aren’t.

websockets provides high-level, coroutine-based APIs. Compared to callbacks, coroutines make it easier to manage control flow in concurrent code.

If you prefer callback-based APIs, you should use another library.

With current Python 3 release the common / suggested way to manage asyncronous coroutines is via asyncio (used by websockets standard library approach).

So, answering your last question (Python vs nodejs), asyncio just allows to manage asyncronous I/O (networking-based in this case) events, as NodeJs does natively. broadley speaking, using asyncio you can achieve I/O websockets performances similar to that obtained using Nodejs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Giorgio Robino