Hello, I’m excited to share the mojo-websockets library for building websockets in Mojo in a similar way for those who are use to the Python websockets library.
Features
WebSocket Server and Client: Supports creating both WebSocket servers and clients.
Compatibility: API designed to be intuitive for developers familiar with the Python websockets library.
Sans/IO Layer: Implements a WebSocket Sans/IO layer pure Mojo and performs no I/O of its own.
The main TO-DO is the non-blocking communications support, as it’s waiting for the official Mojo async support.
Any feedback and contributions are highly appreciated!
Unfortunately, without proper async support in Mojo, the WebSocket server is not usable. In Lightbug, at least, the HTTP connections are quick if there is no keep-alive. However, websocket connections remain active for a whole lifetime.
The thread context was deleted by Mojo and I don’t know how to properly fix it. I used the mark_destroyed trickery but I don’t know if there is a better approach (it should)
There is a segfault related to the CPython interpreter. Don’t know how to fix it.
Most kinds of threading are going to break a lot of things. There’s no way to take the gil right now, so you can’t call python code. You also can’t access global variables.
I think it would be better to try to set up epoll or io_uring and take event handlers.
The support of concurrent connections is already implemented in the io_uring branch. It’s blocked because of some PR for fixing the io_uring wrapper on the latest Max version, but it will be merged soon.