Prepared Messages for Efficient Sending and Publishing
A feature from v0.14, previously removed due to lack of interest, has now been revived! Check out the demo in examples/Precompress.cpp
to see it in action.
A PreparedMessage
is a message that is compressed once but can be sent multiple times. This requires the SHARED_COMPRESSOR
option but works with any decompressor option. The core idea is to decouple the compression process from the act of sending the message, reducing the per-socket computational overhead of compression to near zero.
Ideal Use Case
This feature is ideal for scenarios where you need to send snapshots of a data stream to multiple recipients. Here’s how it could work:
- A timer periodically creates a new
PreparedMessage
containing the latest snapshot of the data (e.g., every 500ms). - When a WebSocket connects, it receives the most recent prepared snapshot, followed by incremental updates (deltas) from that point onward.
- If the snapshot is large, pre-compressing it as a
PreparedMessage
eliminates the need for repeated compression, significantly reducing overhead for each socket.
Important Notes
- This feature only works with
SHARED_COMPRESSOR
. Using a sliding window compression method is incompatible with this optimization. - New methods have been added to support this feature:
WebSocket::sendPrepared
App::publishPrepared
Loop::prepareMessage
- You can create
PreparedMessage
objects on any thread, as long as that thread has its ownLoop
(which is automatically created for you viaLoop::get()
). However, in such cases, you must handle synchronization manually. Refer toexamples/Precompress.cpp
for guidance.
This release introduces the interface and its initial implementation. While memory-saving optimizations are not included in this release, they may be addressed in future updates.