2
I have a nodejs application that needs to consume an endpoint that returns a text/event-stream
. The application that issues these events is done with java and packages a data within an object ServerSentEvent
containing id, Event, Retry, comment and date, where data
is my message.
To consume endpoint I tried to use some lib from eventSource
but none worked as expected. Now I’m trying to consume using the lib http
, thus:
http.get({
agent: false
, path: "/streaming"
, hostname: "localhost"
, port: 8080
}, (res) => {
res.on('data', data => {
console.log(data);
})
});
Observing the object data
that is returned, I notice that it is of the type Buffer. However I can not convert it to a format that I can manipulate.
What should be the right way to consume this endpoint? Keep searching for a lib of EventSource
or there is a way to consume using lib http
?
PS: The Node application does not load anything in the browser, it is just backend. Node V10.16.3
Edited
An example project containing the possible test media can be seen here: https://github.com/josecarlosweb/sse-emit-test
Can you provide a minimum verifiable example? Because if the ones you tested didn’t work it must be due to some particularity of your API
– Sorack
Great idea @Sorack. Creating here now.
– touchmx
I added an example project
– touchmx
If it is a stream, the information does not arrive all at once, you need to gradually consume the buffer. How to do this depends on the library you’re using on the client side, but I imagine that everyone who supports streams offers a way to deal with it.
– bfavaretto