reactor / reactor-core

Non-Blocking Reactive Foundation for the JVM
http://projectreactor.io
Apache License 2.0
4.93k stars 1.2k forks source link

`EventSource`, `Flux<String>`, and `data:` (without a space) #3849

Closed seguri closed 2 months ago

seguri commented 2 months ago

Hi everybody. I'm opening an issue here because I'm using reactor.core.publisher.Flux in my code and the package name brought me here. If I'm not mistaken, your software is used in Spring Boot to implement reactive programming.

I came to Flux<String> while reading "Spring AI in Action", that guided me through building my own AI chat bot with ollama. I decided to implement something I use often, i.e. translating text from one language to another.

My code is very simple:

@Service
class TranslationService(builder: ChatClient.Builder) {
  private val chatClient: ChatClient = builder.build()
  fun translateStream(message: String, from: String, to: String): Flux<String> {
    return chatClient.prompt().system(getTemplate(from, to)).user(message).stream().content()
  }
}

I have a REST controller that exposes this service. Then I built a frontend for this by creating a super simple HTML page with an input field and a submit button that uses an EventSource to query my endpoint. At this point I've spent hours trying to understand why my frontend was not receiving whitespaces. I tested my backend with curl and httpie, and they showed:

HTTP/1.1 200
Content-Type: text/event-stream

data:La
data: frase
data: "
data:come
data: st
data:ai
data:?"

while my frontend was receiving (open Developer Tools, Network tab, click on the request, and then click on the "Response" or "Event Stream" tab):

La
frase
"
come
st
ai
?"

After hours I stumbled upon 9.2.6 Interpreting an event stream where the last example ends with:

This is because the space after the colon is ignored if present.

I've suddenly realized that the problem was the missing space after the colon. There should be always one, and when a chunk starts with a space, there should be two. In my example, Spring Boot should return a double space and not one, i.e. data: frase.

I'm opening this issue to ask for guidance. Who should I forward this issue to? Is this project responsible for how Flux is emitting that data:?

My code is here. I'm not pointing you to any particular commit hash cause I'm overwriting the history to make it more readable. The commit should be named feat: chapter 2 though.

Thank you for your attention.

chemicL commented 2 months ago

I reached out to the spring-ai maintainers. They suggest to open an issue in their repository. It is not caused by anything in reactor-core.

seguri commented 2 months ago

Thanks for asking. New issue is here.