WebSocket client without async
Project description
Python package for simple and easy to use WebSocket clients without async.
Motivation
There are many asynchronous Python WebSocket client packages out there, and almost of them require your code to use the async syntax. This one does not, so it’s easier to use from within a Jupyter Notebook. All of the async code is executed onto a separate thread.
Installation
Install using pip
pip install nwebsocket
Python version
Python 3.7+ is required.
Usage
A simple minimal API interface can be created using the following example.
# minimal.py
from nwebsocket import WebSocket
wscn = WebSocket( "ws://localhost:8001" )
wscn.onmessage = lambda m: print( m )
wscn.onopen = lambda: print( "Opened connection" )
wscn.onclose = lambda: print( "Closed connection" )
wscn.onerror = lambda: print( "Connection errored out" )
print( wscn.readyState )
Inspiration
This package was inspired by the ultra-simple WebSocket API in the JavaScript language which it replicates.
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/url
Guideline
Since the callback-style API is quite unusual when it comes to pythonicity, your task will be to:
define onmessage, onopen, onclose and onerror
handle reconnection/s
implement the TX/RX specification for working with the endpoint
isolate the callback pattern from the rest of your code
Take a chat service as an example, there are operations which are following the traditional request/response pattern such as posting messages, and there are other messages which are received without issuing a request (chat posting by other users).
These latter messages must be handled by your code as soon as they are received. Class instances can help with that, by storing the received information (chat posts). Keep your callbacks short, fast and serializable.
Limitations
This library is not suitable for high throughput, as the queue mechanism in Python is notoriously slow due to serialization.
License (MIT)
Copyright (C) 2022 Adapta Robotics | MATT Robot
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for nwebsocket-0.9.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00da35d6453e95c39ec6ae5180dd15988666389c63c971935d78a0cb698d0db9 |
|
MD5 | cda735e3962a66d316b1b7f57114ee5b |
|
BLAKE2b-256 | 8ddecd074ea8bc162793d815722df8cca16e3c361e24e0f4d02238b54689b264 |