Skip to content

Parsing of ServerSentEvents not working correctly when using reactive WebClient [SPR-16735] #21276

@spring-projects-issues

Description

@spring-projects-issues

Harmen Renkema opened SPR-16735 and commented

We are running into some issues when streaming objects using WebClient from spring-webflux.

We've tracked them to the stream parsing in ServerSentEventHttpMessageReader.read(..).

The issues we're encountering:

  1. for every event supplied by the backend we get 2 ServerSentEvents from WebClient. One populated with our data, and one fully populated with null values. (detailed example data below)
  2. when the events supplied by the backend have newlines in their data section, every now and then the eventstream is split incorrectly. This results in incomplete json being fed into jackson, causing it to throw a JsonEOFException. This occurs at least once every couple of thousand of messages.

Example for the first issue:
We have a backend that provides the following stream.

id:1
event:myEvent
data:{
data:  "id" : 1,
data:  "type" : "myEvent",
data:  "body" : "{ \"drink\": \"coffee\", \"snack\": \"muffin\"}",
data:  "timestamp" : "2018-04-17T07:19:44.769Z"
data:}

id:2
event:myEvent
data:{
data:  "id" : 2,
data:  "type" : "myEvent",
data:  "body" : "{ \"drink\": \"coffee\", \"snack\": \"muffin\"}",
data:  "timestamp" : "2018-04-17T07:19:58.859Z"
data:}

id:3
event:myEvent
data:{
data:  "id" : 3,
data:  "type" : "myEvent",
data:  "body" : "{ \"drink\": \"coffee\", \"snack\": \"muffin\"}",
data:  "timestamp" : "2018-04-17T07:19:59.313Z"
data:}

When I use WebClient like this:

webClient.get()
	.accept(TEXT_EVENT_STREAM)
	.retrieve()
	.bodyToFlux(new ParameterizedTypeReference<ServerSentEvent<Object>>() {})
	.subscribe(System.out::println);

I end up with the following ServerSentEvents:

ServerSentEvent [id = '1', event='myEvent', retry=null, comment='null', data={id=1, type=myEvent, body={ "drink": "coffee", "snack": "muffin"}, timestamp=2018-04-17T07:19:44.769Z}]
ServerSentEvent [id = 'null', event='null', retry=null, comment='null', data=null]
ServerSentEvent [id = '2', event='myEvent', retry=null, comment='null', data={id=2, type=myEvent, body={ "drink": "coffee", "snack": "muffin"}, timestamp=2018-04-17T07:19:58.859Z}]
ServerSentEvent [id = 'null', event='null', retry=null, comment='null', data=null]
ServerSentEvent [id = '3', event='myEvent', retry=null, comment='null', data={id=3, type=myEvent, body={ "drink": "coffee", "snack": "muffin"}, timestamp=2018-04-17T07:19:59.313Z}]
ServerSentEvent [id = 'null', event='null', retry=null, comment='null', data=null]

Affects: 5.0.4

Issue Links:

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions