Skip to main content

Bringing WebSockets, Http/Https High Performance servers for PyPy3 and Python3

Project description

socketify.py

Logo


GitHub Clones PyPI Downloads Discord

:bookmark_tabs: Documentation

See the full docs in docs.socketify.dev or in /docs/README.md

Also take a look in the examples in /examples

💡 Features

  • WebSocket with pub/sub support
  • Fast and reliable Http/Https
  • Support for Windows, Linux and macOS Silicon & x64
  • Support for PyPy3 and CPython
  • Dynamic URL Routing with Wildcard & Parameter support
  • Sync and Async Function Support
  • Really Simple API
  • Fast and Encrypted TLS 1.3 quicker than most alternative servers can do even unencrypted, cleartext messaging
  • Per-SNI HttpRouter Support
  • Proxy Protocol v2
  • Shared or Dedicated Compression Support
  • Max Backpressure, Max Timeout, Max Payload and Idle Timeout Support
  • Automatic Ping / Pong Support
  • Per Socket Data
  • Middlewares
  • Templates Support (examples with Mako and Jinja2)
  • ASGI Server
  • WSGI Server
  • Plugins/Extensions

:mag_right: Upcoming Features

  • In-Memory Cache Tools
  • Fetch like API powered by libuv
  • Async file IO powered by libuv
  • Full asyncio integration with libuv
  • SSGI Server spec and support
  • RSGI Server support
  • Full Http3 support
  • HPy integration to better support CPython, PyPy and GraalPython
  • Hot Reloading

We created and adapted the full C API from uNetworking/uWebSockets and will integrate libuv powered fetch and file IO, this same C API is used by Bun

Join Github Discussions or Discord for help and have a look at the development progress.

:zap: Benchmarks

Socketify WebFramework HTTP requests per second (Linux x64)

image

WSGI Server requests per second (Linux x64)

image

ASGI Server requests per second (Linux x64)

image

WebSocket messages per second (Linux x64)

image

Http tested with TFB tool plaintext benchmark
WebSocket tested with Bun.sh bench chat-client
Source code in TechEmPower and for websockets in bench

Machine OS: Debian GNU/Linux bookworm/sid x86_64 Kernel: 6.0.0-2-amd64 CPU: Intel i7-7700HQ (8) @ 3.800GHz Memory: 32066MiB

📦 Installation

For macOS x64 & Silicon, Linux x64, Windows

pip install socketify
#or specify PyPy3
pypy3 -m pip install socketify
#or in editable mode
pypy3 -m pip install -e socketify

Using install via requirements.txt

socketify
pip install -r ./requirements.txt 
#or specify PyPy3
pypy3 -m pip install -r ./requirements.txt 

If you are using linux or macOS, you may need to install libuv and zlib in your system

macOS

brew install libuv
brew install zlib

Linux

apt install libuv1 zlib1g

🤔 Usage

Hello world app

from socketify import App

app = App()
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
app.run()

SSL version sample

from socketify import App, AppOptions

app = App(AppOptions(key_file_name="./misc/key.pem", cert_file_name="./misc/cert.pem", passphrase="1234"))
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
app.run()

WebSockets

from socketify import App, AppOptions, OpCode, CompressOptions

def ws_open(ws):
    print('A WebSocket got connected!')
    ws.send("Hello World!", OpCode.TEXT)

def ws_message(ws, message, opcode):
    #Ok is false if backpressure was built up, wait for drain
    ok = ws.send(message, opcode)
    
app = App()    
app.ws("/*", {
    'compression': CompressOptions.SHARED_COMPRESSOR,
    'max_payload_length': 16 * 1024 * 1024,
    'idle_timeout': 12,
    'open': ws_open,
    'message': ws_message,
    'drain': lambda ws: print(f'WebSocket backpressure: {ws.get_buffered_amount()}'),
    'close': lambda ws, code, message: print('WebSocket closed'),
    'subscription': lambda ws, topic, subscriptions, subscriptions_before: print(f'subscription/unsubscription on topic {topic} {subscriptions} {subscriptions_before}'),
})
app.any("/", lambda res,req: res.end("Nothing to see here!'"))
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port)))
app.run()

We have more than 20 examples click here for more

:hammer: Building from source

#clone and update submodules
git clone https://github.com/cirospaciari/socketify.py.git
cd ./socketify.py
git submodule update --init --recursive --remote
#you can use make linux, make macos or call Make.bat from Visual Studio Development Prompt to build
cd ./src/socketify/native/ && make linux && cd ../../../
#install local pip
pypy3 -m pip install .
#install in editable mode
pypy3 -m pip install -e .
#if you want to remove
pypy3 -m pip uninstall socketify

:briefcase: Commercially supported

I'm a Brazilian consulting & contracting company dealing with anything related with socketify.py and socketify.rb

Don't hesitate sending a mail if you are in need of advice, support, or having other business inquiries in mind. We'll figure out what's best for both parties.

Special thank's to uNetworking AB to develop uWebSockets, uSockets and allow us to bring this features and performance to Python and PyPy

:heart: Sponsors

If you like to see this project thrive, you can sponsor us on GitHub too. We need all the help we can get.

Thank you Otavio Augusto to be the first sponsor of this project!

:star: Stargazers

Stargazers repo roster for @cirospaciari/socketify.py

:wrench: Forkers

Forkers repo roster for @cirospaciari/socketify.py

:question: socketify.py vs japronto

People really want to compare with japronto, but this projects are not really comparable. Socketify is an active project and will be maintained over time with security updates and new features, japronto don't get any github updates since 2020 and don't get any src update since 2018, japronto don't support SSL, WebSockets, PyPy3, Windows or macOS Silicon, socketify will support Http3 and a lot more features.

And yes, we can be faster than japronto when all our features and goals are achieved, and we are probably faster than any current maintained solution out there.

:grey_question: uvloop

We don't use uvloop, because uvloop don't support Windows and PyPy3 at this moment, this can change in the future, but right now we want to implement our own libuv + asyncio solution, and a lot more.

:dizzy: CFFI vs Cython vs HPy

Cython performs really well on Python3 but really bad on PyPy3, CFFI are chosen for better support PyPy3 until we got our hands on an stable HPy integration.

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

socketify-0.0.20.tar.gz (5.5 MB view details)

Uploaded Source

Built Distributions

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

socketify-0.0.20-pp39-pypy39_pp73-any.whl (5.6 MB view details)

Uploaded PyPy

socketify-0.0.20-cp311-cp311-any.whl (5.6 MB view details)

Uploaded CPython 3.11

File details

Details for the file socketify-0.0.20.tar.gz.

File metadata

  • Download URL: socketify-0.0.20.tar.gz
  • Upload date:
  • Size: 5.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for socketify-0.0.20.tar.gz
Algorithm Hash digest
SHA256 766c6e9ae35c523c7b02bf67ccc97ac79e35fc1d4bfa7b18d11ec7374c955e96
MD5 28be4dba3539907fd7ee0b3fe8dd2ba3
BLAKE2b-256 de190250ffa1f0d949c30d0cea7e0fb97d56e60734039ddfdd81341b5e1a8365

See more details on using hashes here.

File details

Details for the file socketify-0.0.20-pp39-pypy39_pp73-any.whl.

File metadata

File hashes

Hashes for socketify-0.0.20-pp39-pypy39_pp73-any.whl
Algorithm Hash digest
SHA256 fe713ab2d630365f04e9475cf26e9f3a61ff507521a747eb10449ae14f0977f8
MD5 41d88fe76d412dc78b25894a22047e63
BLAKE2b-256 66074ac3d599ce94e751a2362f168a4059e2fad17aabfca4d5276f21eccbf87b

See more details on using hashes here.

File details

Details for the file socketify-0.0.20-cp311-cp311-any.whl.

File metadata

  • Download URL: socketify-0.0.20-cp311-cp311-any.whl
  • Upload date:
  • Size: 5.6 MB
  • Tags: CPython 3.11
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for socketify-0.0.20-cp311-cp311-any.whl
Algorithm Hash digest
SHA256 58693d34f9622e33edf9d206451b9a188c6a57074cb171857150198e390be574
MD5 6422b38d3c12be6631b5a66975a13716
BLAKE2b-256 33f6223da6253fa753144cdc2f51cfc2c2c3225c63d4d18d34267a732df2e907

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