Mojo's websockets library
README
mojo-websockets
is a lightweight library for handling WebSocket connections in Mojo.
It aims to provide an similar interface as the python-websockets package for creating WebSocket servers and clients, with additional features for enhanced usability.
This software is in a early stage of development. Please DO NOT use yet for production ready services.
For a complete listing, see the features document.
Install pixi
Add the WebSockets Package (at the top level of your project):
pixi add websockets
from websockets.sync.server import serve, WSConnection
fn on_message(conn: WSConnection, data: Span[Byte]) raises -> None:
str_received = String(data)
print("<<< ", str_received)
conn.send_text(str_received)
print(">>> ", str_received)
fn main() raises:
with serve[on_message]("127.0.0.1", 8000) as server:
server.serve_forever()
from websockets.sync.client import connect
fn send_and_receive(msg: String) raises:
with connect("ws://127.0.0.1:8000") as client:
client.send_text(msg)
print(">>> ", msg)
response = client.recv_text()
print("<<< ", response)
fn main() raises:
send_and_receive("Hello world!")
io_uring
branch)See all the remaining features in the features document.
Contributions are welcome! If you'd like to contribute, please follow the contribution guidelines in the CONTRIBUTING.md file in the repository.
We have taken a lot of code from the amazing lightbug_http project.
Also, we took inspiration and some code from the python-websockets project, specially for implementing the WebSocket Sans/IO layer and their tests.
mojo-websockets is licensed under the MIT license.
DETAILS