-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
On changing the project architecture and fixing bugs #19
Comments
Does your fork work in web? :) Thanks for sharing your work! |
@nietsmmar On one hand, It looks like Dart's As for the fork, I didn't take web support into consideration when updating the code because I didn't need it. However, it supports using a custom HTTP client of package:http via the final client = SseClient(
http.Request('GET', Uri.parse('http://example.com/subscribe'))
..headers.addAll({
'Authorization': 'Bearer (token)',
'Cache-Control': 'no-cache',
}),
httpClientProvider: () => FetchClient(mode: RequestMode.cors),
); I just tested and it works. But I didn't do much testing on the web, so more feedback and contributions are needed. |
@tamcy Thank you so much. I will try this and report back. |
EDIT
Because I am streaming chatGPT OpenAI chunks. They often lead with whitespaces. Without them, I just get the text without any whitespaces. Is there a reason why they are removed? Same problem with new lines at the end of incoming events. Maybe it is this paft of code. Although when editing this line to not remove line breaks, the data weirdly has more whitespace and the line break is still missing.
|
@nietsmmar The message interpretation is implemented according to what is specified in the standard. Again, quote https://html.spec.whatwg.org/multipage/server-sent-events.html: If the server side sends the message in a way that expects the first whitespace after the colon be preserved, it is violating the specification and it is the server code, not the client code that should be fixed. Same for ending line breaks. |
@tamcy Thank you for the reply. I see. I just forwarded what I get from |
It would be great to return the original error in the
Because now the |
First off, thank you for your package.
Long story short: I found your package useful, but the current implementation doesn't suit my need, and I was in urgent need of implementing something with SSE. So I copied the code locally, hacked into it, and finished my job first. Now I would like to contribute my changes back.
After forking and trying to update the code into your original repo, I feel hesistated, because I have made substantial changes to the original library, and there is no backward compatibility. So I would like to know your take on this before continuing, or submitting a pull request.
Here is my updated repo:
https://github.com/tamcy/dart_sse_client/tree/tamcy-dev
Example can be found here (
README has not been updatedUpdate: now it's updated):https://github.com/tamcy/dart_sse_client/blob/tamcy-dev/example/main.dart
It's not finished for a pull request, but the code should work.
The changes are aimed to:
Here is the longer summary:
Fixing architectural issues
Currently, calling
subscribeToSSE()
twice will cause the originalhttp.Client
being lost, and it's not possible to close the connection of previous http clients (not to mentione there's no way to close a stream). Which is why I made the SSE client an instance class.I see
SseClient
something similar to but not the same as an HTTP client. An HTTP client sends a request and returns a response, while an SSE "client" should act more like a wrapper class that encapsulates the details of an SSE connection. With this in mind I have moved the connection details to the constructor. In my design, an SseClient instance will only be responsible of managing the connection of a single request. User should create a newSseClient
instance for a different requests.Also, while an SSE client needs a
http.Request
object forhttp.Client
, I think it should not be opinionated on how the request object is constructed and the body format (the current library is json-encoding it). So instead of asking for the request method, URL, header and body, the updated version now asks for anhttp.Request
object. This also elminates the need for anSSERequestType
enum.Fixing bugs
I found that the current implementation didn't quite follow the specification on message parsing, especially on how the white spaces and line breaks are handled. Imagine when the message is in plain text and you need to compare the string data. This has been fixed in my fork.
My fork also checks for the response's
Content-Type
according to the spec:Coding style fixes
I have also updated the code to make it more Dart-like, at least as per my understanding. For instance, I have separate the published event object from the one that is used for buffering. To me, the buffering object is an internal representation which, while very similar to the dispatch event in structure, should not be exposed to users. In particular, the fields of the emitted message should be immutable.
I have also updated the
CHANGELOG.md
file so that it shows the changes in descending order. I believe this is a more common practice.This version should close #10, #18, and replaces pull requests #7, #11 and #15.
Others
One missing feature in the updated package is the ability to auto reconnect when fail, because I didn't need it. I am not sure if I can take the time to do it, but I will wait for your response first.Update: Auto reconnection can be achieved using the new
AutoReconnectSseClient
class.Besides, I'd also like to suggest changing the package name to something like "dart_sse_client" as the package doesn't depend on Flutter. Also, using a branch name like
dev
instead ofuat
should be less confusing.Thanks for you time!
The text was updated successfully, but these errors were encountered: