Skip to content
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

Safari doesn't receive chunked data using WebFlux's SSE [SPR-16691] #21232

Closed
spring-projects-issues opened this issue Apr 4, 2018 · 1 comment
Closed
Assignees
Labels
status: invalid

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Apr 4, 2018

Adrian Marszałek opened SPR-16691 and commented

Complete source code:

@Configuration
class Routing {

    @Bean
    fun booksRouter(handler: BooksHandler) = router {
        ("/books" and accept(MediaType.APPLICATION_JSON)).nest {
            GET("/", handler::getAll)
        }
    }
}


data class Book(val title: String,
                val author: String)

@Component
class BooksHandler(private val repository: BookRepository) {

    fun getAll(request: ServerRequest): Mono<ServerResponse> {
        val interval = Flux.interval(Duration.ofSeconds(1))

        val books = listOf(Book("title", "author"),
                Book("title", "author"),
                Book("title", "author"),
                Book("title", "author")).toFlux()
        return ok().bodyToServerSentEvents(Flux.zip(interval, books).map({ it.t2 }))
    }
}

The whole project generated from Intializr: Spring Boot 2.0.0 with Kotlin 1.2.31.
Run the application and access the resource directly through Safari: http://localhost:8080/books
Instead of receiving a book every second, Safari waits for as long as the Flux.zip() takes and then it receives the whole list of books.


No further details from SPR-16691

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Apr 4, 2018

Brian Clozel commented

Thanks for the report, but Safari does properly support SSE with WebFlux.

The issue seems to be with your RouterFunction only accepting MediaType.APPLICATION_JSON for this endpoint. This might work with some browsers, if they send an Accept header that's flexible (maybe accepting "\*/\*"?).

Safari seems to be more restrictive and sends an "Accept: text/event-stream" request header for SSE requests done with EventSource. In this case, the server responds with an HTTP 404...

Changing that part in your sample with the following works:

@Bean
fun booksRouter(handler: BooksHandler) = router {
    GET("/", handler::home)
    ("/books" and accept(MediaType.TEXT_EVENT_STREAM)).nest {
        GET("/", handler::getAll)
    }
}

Thanks,

@spring-projects-issues spring-projects-issues added type: bug status: invalid labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug label Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid
Projects
None yet
Development

No branches or pull requests

2 participants