Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "text/event-stream" and "application/stream+json"

@GetMapping(path = "/cars", produces = "text/event-stream")
public Flux<Car> getCarStream() {
    System.out.println("application/stream+json");
    return this.repository.findCarsBy().log();
}

What's the difference between the above code and the following:

@GetMapping(path = "/cars", produces = "application/stream+json")
public Flux<Car> getCarStream() {
    System.out.println("application/stream+json");
    return this.repository.findCarsBy().log();
}

So far I've found the contradictory information: some say they both mean server-sent events and others that there is a difference.

like image 958
Ekaterina Avatar asked Aug 30 '18 13:08

Ekaterina


1 Answers

TL;DR: that dzone article is wrong, and Rossen's talk is right.

text/event-stream is the official media type for Server Sent Events (SSE); it will prefix data bits with a data: prefix and you can also choose your prefix to change the meaning of that piece of data for the client. This media type is for browsers, as they support that using the EventSource JavaScript API.

application/stream+json is for server to server/http client (anything that's not a browser) communications. It won't prefix the data and will just use CRLF to split the pieces of data. Note that the Spring team is reconsidering that media type, because SPR-16742 (don't hesitate to comment there!).

Update: application/x-ndjson is now the preferred media type for streaming JSON values.

like image 183
Brian Clozel Avatar answered Nov 13 '22 21:11

Brian Clozel