Git Product home page Git Product logo

quicksocket's Introduction

quicksocket

MIT licensed Apache v2 licensed Actions Status PyPI Crates.io

A simple WebSocket server built in Rust using tokio, tokio-tungstenite, and pyo3. Supports Windows, macOS, and Linux.

More stable as of 1.0.0, but still feature-light. You only get one server (need to add a proper Server class), and the features are pretty minimal.

pip install quicksocket

Quick Start for Quicksocket

quicksocket.server.Server provides type annotations for the otherwise static methods available directly from the quicksocket module.

import quicksocket

# Start the server on whatever port you want.
server = quicksocket.server.Server()
server.start(port=59994)

# You have to poll the server, which runs on a native Rust thread.
# 
# No need for `asyncio` here! Do it however you want.
# But you'll need some sort of loop for this.
new_clients = server.drain_new_client_events()
cli_msgs = server.drain_client_messages()

# Send messages in batches for better efficiency. Often, python's "threading" is a performance bottleneck.
message = "Hello, world!"
another_message = "Yes, hello!"
server.send_messages([message, another_message])

# Check if the server is running.
is_server_running = server.is_running()

# Stop the server.
server.stop()

A bit verbose, and still stabilizing.

As of 1.0 the initial connection port is configurable, just pass the port to the start method.

Quicksocket's code is originally designed for use with Ultraleap's Web Visualizer project, and as such is intended for a console python visualizer server and leverages plain println!s for logging purposes. Coming "soon": Proper env logging.

Building

You'll need Rust and access to some python.exe of version 3.6 or greater.

You'll also need OpenSSL. See the Ubuntu section for installation details on Ubuntu. OpenSSL is slightly trickier for Windows. You can use Chocolatey or vcpkg, or download a binary distribution of OpenSSL. You may need to set $Env:OPENSSL_DIR (PowerShell syntax) to your installation directory for your Windows build session if using Chocolatey, or a binary install, or if vcpkg isn't activated for your session. macOS should have it by default.

To build a wheel for your python/OS combo:

pip install maturin
maturin build

If you just want a raw .pyd or similar python native module file for your OS, you can just build with cargo:

cargo build --release

There's CI for Windows, macOS, and Linux for Pythons 3.6 through 3.9. Check out the Actions tab.

Ubuntu

Make sure you have libssl and libpython installed:

sudo apt-get install libssl-dev
sudo apt-get install libpython3-dev

If you encounter errors building pyo3, you may need to check whether it can find your python and any related python dev dependencies: https://pyo3.rs/v0.10.1/building_and_distribution.html

Older Build Instructions

Targeting builds to specific Python versions

This only pertains to building via cargo. maturin is probably more reliable.

If you're targeting a specific python version, or if you don't have python on your PATH, you can set PYTHON_SYS_EXECUTABLE to the python executable in your machine with that version.

e.g., on Windows via Git Bash:

PYTHON_SYS_EXECUTABLE=/c/my/path/to/python.exe cargo build --release

Todos:

A utility HTML via github pages

Could expose this through Github Pages so it's easy for a new user to test their server usage... https://github.com/nickjbenson/quicksocket/blob/main/archive/examples/wip01_basic_websocket_client.html

quicksocket's People

Contributors

vabrador avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.