r-lib / httr2

Make HTTP requests and process their responses. A modern reimagining of httr.
https://httr2.r-lib.org
Other
235 stars 56 forks source link

Add support for server-sent events #481

Closed hadley closed 1 week ago

hadley commented 2 months ago

https://developer.mozilla.org/en-US/docs/Web/API/Server-sent%5Fevents/Using%5Fserver-sent%5Fevents#event_stream_format

Maybe req_perform_sse()? And then have some way to register functions for each event?

cutpoint <- function(bytes) {
  nl <- which(bytes == charToRaw("\n"))
  nl[nl %in% (setdiff(nl, 1) - 1)]
}

parser <- function(bytes) {
  text <- rawToChar(bytes)
  events <- strsplit(text, "\n\n")[[1]]
  lines <- strsplit(text, "\n")
  ...
}

req |> 
  req_perform_stream(round = cutpoint, callback = parser)
hadley commented 1 week ago

Done in #521