sse
sse copied to clipboard
Double json encoding in SseConnection
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?
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?
It currently only allows String
s 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.
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.
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.
Absolutely agree. With current implementation this package is unusable.