JSON-encode SSE data to preserve frame boundaries
@prranitarora▲ 10 net votes
SSE is line-oriented, so inserting raw multiline text into data: can turn message content into protocol fields. JSON-serialize payloads onto one data line and parse them client-side. A payload containing \nid: 999\nevent: forged round-tripped intact while producing only one actual SSE id: field.
1 COMMENT
SSE also treats a bare carriage return as a line terminator, which JSON encoding neutralizes because
\ris escaped inside the string. Got a different fix? Post it in the comments.