sse icon indicating copy to clipboard operation
sse copied to clipboard

Double json encoding in SseConnection

Open spkersten opened this issue 2 years ago • 5 comments

The type of SseConnection.sink is Stream<String>but then before sending (in _setUpListener) the message is JSON encoded. The interface I try to implement expects singly encoded JSON. Could the type of SseConnection.sink be relaxed to Stream<Object> so any JSON-encodable object can be sent?

spkersten avatar Jan 11 '23 13:01 spkersten

The message is JSON encoded before sending over the wire. We decode it into a String here: https://github.com/dart-lang/sse/blob/be426a2467108d88354dc36a44493d2d17019993/lib/client/sse_client.dart#L126

Changing this behavior would be a breaking change. Note the resulting String should be singly encoded so I'm not sure what the issue is. Can you provide more details?

grouma avatar Jan 11 '23 21:01 grouma

It currently only allows Strings to be sent. I'd like to send json object (say, a list of strings). So I use json.encode to make it a String, but then _setUpListener encodes it as json again.

I'm not proposing to remove the json encoding (as it would be a break change) but to change the type of SseConnection.sink to Stream<Object> so that in addition to Strings it accepts any json-encodable object.

spkersten avatar Jan 12 '23 08:01 spkersten

The incoming type will have to change which is a breaking change: https://github.com/dart-lang/sse/blob/be426a2467108d88354dc36a44493d2d17019993/lib/client/sse_client.dart#L94

The String you get on the otherside can be decoded once again to get the obj you want.

grouma avatar Jan 12 '23 16:01 grouma

I would propose to introduce this breaking change. It makes using the library more difficult, especially for newcomers. Also, you would not expect this behaviour according to the standard. Why not stick to https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events ?

I wondered also, why there are "Control Messages" like "close" to close the connection. Ideally, this is not done by the library, but by the user. Also, SSE is intended as a one way data transfer. I cannot follow these design decisions, even though they are stated in the Readme.

//Edit: If the design decisions of this package cannot be followed, maybe it is better to use EventSource from dart:html directly instead.

ratzrattillo avatar Jun 03 '23 11:06 ratzrattillo

Absolutely agree. With current implementation this package is unusable.

radzish avatar Jun 29 '23 09:06 radzish