Cutelyst 1.7.0 released! WebSocket support added.
Cutelyst the Qt / C++11 web framework just got another important release.
WebSocket support is probably a key feature to have on a modern web framework, Perl Catalyst doesn't look like it wasn't designed with it in mind, the way I found to do WS there wasn't intuitive.
Qt has WebSocket support via QWebSockets module, which includes client and server implementation, and I have used it for a few jobs due lack of support inside Cutelyst. While looking at it's implementation I realized that it wouldn't fit into Cutelyst, and it even had a TODO due a blocking call, so no go.
I've looked then at uWSGI implementation and while it is far from being RFC compliant or even usable (IMO) the parser was much simpler, but it required that all data was available to parse. Since messages can be split into frames (really 63bits to address payload was probably not enough) and each frame has a bit to know if the message is over, which uWSGI simply discards, thus any fragmented message can't be merged back.
WebSockets in Cutelyst have an API closer to what QWebSocket has, but since we are a web framework we have more control over things, first on your websocket endpoint you must call:
c->response->webSocketHandshake()
If the return is false just return from the method to ignore the request or it might be the case that you want the same path to show a chat HTML page, this can happen if the client didn't sent the proper headers or if the Engine backend/protocol doesn't support WebSockets. If true you can connect to the Request object and get notified about pong, close, binary and text (UTF-8) messages and frames, a simple usage is like:
connect(req, &Request::webSocketTextMessage, [=] (const QString &msg) { qDebug() << "Got text msg" << msg; response->webSocketTextMessage(msg); });
This will work as a Echo server, the signals also include Cutelyst::Context in the case you are connecting to a slot.
Cutelyst implementation is non-blocking, a little faster than QWebSockets, uses less memory and passes all http://autobahn.ws/testsuite tests, it doesn't support compression yet.
systemd socket activation support was added, this is another very cool feature although it's still missing a way to die the application on idle. On my tests with systemd socket activation, Cutelyst started so fast that the first request that starts it takes only twice the time it would take if Cutelyst was already running.
This release also include:
- Fixes for using cutelyst-wsgi talking FastCGI to Apache
- Workaround QLocalServer closing down when an accept fail (very common when used on prefork/threaded servers)
- Fixed when using cutelyst-wsgi on threaded mode, POST requests would use the same buffer corrupting or crashing.
- A few other fixes.
Due WebSockets plans for Cutelyst 2 are taking shape, but I'll probably try to add HTTP2 support first since the multiplexing part is might make Cutelyst Engine quite complex.
Have fun https://github.com/cutelyst/cutelyst/archive/v1.7.0.tar.gz