how_to_optimize_publish_subscribe_speed.md•4.55 kB
# How to Optimize Publish and Subscribe Speed with PubNub
Optimizing the speed of publishing and subscribing to messages is key to a high-performance real-time application.
## Publishing Messages
**Publish Rate:**
* Messages can be published as fast as your network, server/device CPU/RAM, and ISP allow.
* **Recommendation:** PubNub recommends publishing 10–15 messages per second per channel for optimal subscriber experience. Subscribers might miss messages if their subscribe rate is too slow to keep up with a very high publish rate, as the server-side channel message queue (default 100 messages) can overflow.
* If you need to publish at higher rates, ensure your subscribers can consume messages quickly or consider strategies like message batching.
**HTTP Streaming & Pipelining:**
* PubNub's network supports sending multiple publish requests on the same TCP socket connection without waiting for a response for each one (HTTP Pipelining).
**Message Payload Size:**
* The maximum message size is 32KB (32,768 bytes).
* Smaller messages transmit faster. Optimize your payload by:
* Sending only essential data.
* Using concise field names in JSON objects.
* Consider using binary data or compressed formats if applicable, though PubNub primarily handles JSON effectively.
* **Calculation:** Use `encodeURIComponent(channel + JSON.stringify(message)).length + 100;` as a rough guide for JSON message size. Always use UTF-8 encoding for accurate estimation.
**Batching Messages:**
* Instead of sending many small, individual messages in rapid succession, consider batching them into a single, larger message (up to 32KB). This reduces the number of publish operations and can improve overall throughput and efficiency.
* PubNub Functions can also be used to aggregate or transform messages on the server-side before they are published or after they are received.
**Asynchronous Publishing:**
* Most PubNub SDKs offer asynchronous publish methods. Utilizing these prevents your application from blocking while waiting for a publish confirmation, allowing it to send more messages concurrently.
## Subscribing to Messages
**Subscriber Consumption Rate:**
* Consider how quickly a subscriber can realistically process incoming messages.
* **Human Readers:** Avoid overwhelming users with too many messages.
* **Data-Driven Graphs:** Determine the minimum messages per second needed for a smooth graphing experience.
* If subscribers can't keep pace with publishers, messages might be missed if the in-memory queue (default 100 messages per channel/multiplexed connection) overflows. You can contact PubNub support to discuss increasing the message queue limit for your sub-key if needed.
**Efficient Subscriptions with Stream Controller:**
* **Multiplexing:** Subscribe to multiple channels (PubNub recommends up to 30, possibly fewer for larger apps) over a single TCP connection. This is more efficient than opening a separate connection for each channel.
* **Channel Groups:** For subscribing to a larger number of channels (up to 2000 channels per group, and up to 10 channel groups per client, totaling 20,000 channels), use Channel Groups. This allows a client to subscribe to many channels with a single declaration, managed server-side.
* **Wildcard Subscribe:** Subscribe to channels matching a pattern (e.g., `news.*`). This is useful for hierarchical channel structures.
* Stream Controller (which includes Channel Groups and Wildcard Subscribe) must be enabled in your PubNub Admin Portal.
**Windowing (JavaScript SDK):**
* Some SDKs, like older JavaScript versions, had a "windowing" feature that allowed the PubNub network time to bundle and compress messages for high-throughput, potentially improving performance for applications sending many messages per second. Check your specific SDK documentation for similar optimization features.
**Connection Management:**
* PubNub SDKs handle connection management, including automatic reconnection. Ensure your application correctly handles connection status events to manage message flow during temporary disconnects.
**Delta Compression/Syncing (Consideration):**
* For use cases involving frequent updates to large data structures, consider sending only the changes (deltas) rather than the entire object with each message. This reduces payload size and improves speed. PubNub itself doesn't natively enforce delta compression on payloads, but you can implement this logic in your application.