sse_events: HTTP event emitter for server-sent events

sse_eventsR Documentation

HTTP event emitter for server-sent events

Description

Server-sent events are a technique to stream events from a web server to a client, through an open HTTP connection.

Details

This class implements an event emitter on an async HTTP query created with http_get() and friends, that fires an "event" event when the server sends an event. An "end" event is emitted when the server closes the connection.

An event is a named character vector, the names are the keys of the events.

Example using our built-in toy web app:

http <- webfakes::new_app_process(async:::sseapp())
stream_events <- function() {
  query <- http_get(http$url("/sse"))
  sse <- sse_events$new(query)
  sse$
    listen_on("event", function(event) {
      writeLines("Got an event:")
      print(event)
    })$
    listen_on("end", function() {
      writeLines("Done.")
    })
  query
}

response <- synchronise(stream_events())

r-lib/async documentation built on March 24, 2024, 6:20 p.m.