shiichan

WebSocket data transfer is now counted correctly!

Hello, it's Shii! Today I found a fix on Cloudflare's Changelog that's a bit low-key but important, so let me share it. If you're running a service that uses WebSockets, this one's worth checking out!

Cloudflare Changelog developers.cloudflare.com

What was announced?

According to Cloudflare's Changelog, an issue affecting how WebSocket connection data transfer was counted has been fixed. HTTP Traffic Analytics and HTTP request logs now correctly count data transferred throughout a WebSocket connection.

Before the fix, some WebSocket connections were only counted during the initial 101 Switching Protocols handshake, which meant the actual amount of data transferred could be underreported.

The story so far

A WebSocket connection starts with a 101 Switching Protocols handshake, and then keeps exchanging messages over that same connection for a long time. But with this bug, only that initial handshake was being recorded, and the actual data exchanged after the connection was established wasn't being counted. So services that use WebSockets heavily may have seen a Data Transfer number on the dashboard that was smaller than what was actually happening.

What changes

From now on, the Data Transfer figure on the dashboard, and the EdgeResponseBytes field in Analytics and HTTP request logs, will correctly reflect the data transferred over the whole lifetime of a WebSocket connection. If you run a service with a lot of WebSocket traffic, you might see the numbers jump after this fix — but that's not because your actual traffic increased. It's just that data which wasn't being counted before is now being counted correctly. The behavior of WebSocket connections themselves is unaffected, so there's nothing to worry about there.

The separate WebSocket Analytics Logpush dataset, which records per-connection directional byte counts, timestamps, and close details, was already accurate, so it's unaffected by this fix.

Wrap-up

  • WebSocket connection data transfer is now counted for the whole connection, not just the handshake
  • The dashboard's Data Transfer and the EdgeResponseBytes field may change because of this fix (not because traffic increased, but because it's now counted correctly)
  • The WebSocket Analytics Logpush dataset was already accurate and is unaffected
  • If you run WebSocket-based services and use Analytics numbers for cost tracking or monitoring, it's worth double-checking your dashboards