I'm writing a simple streaming JSON service. It consists of JSON messages, sent intermittently, for a long period of time (weeks or months).
What is the best practise with regard to sending multiple JSON messages over a plain TCP socket?
Some alternatives I have looked at (and their downsides) are:
- newline separated JSON - downside: newlines within JSON require escaping, or prohibition
- websocket inspired 0x00 0xff framing - downside: it's now binary, not utf-8 anymore
- real websockets - downside: lack of (opensource) websocket client libraries
- http multipart http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html - downside: incomplete client support?
- no delimiters - downside: chunking requires JSON parsing (can't just count curlies because of curlies in strings)
Is there a good, or at least well-established way of doing this?
I've codified what I and some other developers are doing:
http://en.wikipedia.org/wiki/Line_Delimited_JSON
It has the advantage of being netcat/telnet compatible.
See also: http://ndjson.org/