Skip to main content

WebSocket client without async

Project description

Python Package Index PyPI - Python Version Download count Code Coverage Continuous Integration

Simple and easy to use syncronous WebSocket client, no async.

Usable with Jupyter Notebook.

Installation

Install using pip

pip install nwebsocket

Python version

Python 3.7+ is required.

Usage

Raw API

A simple example without classes

# example_minimal.py
import time

from nwebsocket import WebSocket

wscn = WebSocket("wss://ws.postman-echo.com/raw")

wscn.onmessage = lambda m: print(m)
wscn.onopen = lambda: print("Opened connection")
wscn.onclose = lambda: print("Closed connection")
wscn.onerror = lambda e: print("Connection error", e)

while(not wscn.readyState):
    time.sleep(1e-4)

wscn.send('text')
time.sleep(1.)

wscn.close()

Class protocol

Example of extending the WebSocket class.

# example_class.py
import time

from nwebsocket import WebSocket


class WSProtocolLogic(WebSocket):
    def __init__(self, url):
        super().__init__(url)

        self.messages = []

        # wait for connection, close or error
        while(not self.readyState):
            time.sleep(1e-4)

    def onopen(self):
        print("Opened connection")

    def onclose(self):
        print("Closed connection")

    def onerror(self, e):
        print("Connection error", e)

    def onmessage(self, m):
        self.messages.append(m)


wscn = WSProtocolLogic("wss://ws.postman-echo.com/raw")

wscn.send('text')
time.sleep(1.)

print(wscn.messages)

wscn.close()

Motivation

There are many asynchronous Python WebSocket client packages out there, and almost of them require your code to use the async syntax. This is unfortunate, since it will lead to a run_until_complete call eventually, which will block the main thread from performing other operations in parrallel.

Inspiration

This package was inspired by the ultra-simple WebSocket API in the JavaScript language, which it replicates one-to-one.

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

nwebsocket-1.0.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nwebsocket-1.0.0-py37.py38.py39.py310-none-any.whl (7.2 kB view details)

Uploaded Python 3.10Python 3.7Python 3.8Python 3.9

File details

Details for the file nwebsocket-1.0.0.tar.gz.

File metadata

  • Download URL: nwebsocket-1.0.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.61.2 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for nwebsocket-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4c1a01ad4edb55b718c81c365f3aa5740d427d47aa493c07870550896509200f
MD5 f5df9a6607e189fd68d09bf68d8b9a95
BLAKE2b-256 b7a0deacaf7f99b814b6716fc445b262b136277062c99c1cdb1d73d33ccacb0f

See more details on using hashes here.

File details

Details for the file nwebsocket-1.0.0-py37.py38.py39.py310-none-any.whl.

File metadata

  • Download URL: nwebsocket-1.0.0-py37.py38.py39.py310-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3.10, Python 3.7, Python 3.8, Python 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.61.2 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for nwebsocket-1.0.0-py37.py38.py39.py310-none-any.whl
Algorithm Hash digest
SHA256 8d9c94e0789c99e0e97b82ef917a8385e7c538cf989de90b8dc79e95382013ad
MD5 3eba1e56f8e9330b40c75339185d438a
BLAKE2b-256 8a5b6fcc49193f7b8c52f9dab03c6d6d3b61a0d730726abdbe07a7ecefc8ad46

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page