Sends one request message and returns a stream that yields the response messages one at a time, as they arrive. The server sends only as far ahead as you read, so a stream you stop reading holds the server back rather than filling memory.
Arguments
- channel
A channel from
grpc_channel().- method
The full method name,
"/package.Service/Method".- request
The encoded request message, a raw vector.
- metadata
Request metadata: a named character vector, or a named list whose
-binentries are raw vectors. Names are lower-cased. Names starting withgrpc-or:are reserved, and so arete,content-type,content-length,accept-encoding,hostand the connection headers HTTP/2 forbids, such asconnection. Each name may be given once. Auser-agentis sent before zugrpc's own.- deadline
Seconds from now, or an absolute
POSIXct, by which the whole stream must end. It is sent asgrpc-timeout. Locally it is checked before each read, and each wait for more data is bounded by the time the deadline allowed when the call began, so a stall can overrun a deadline that is already partly used.NULLsets no deadline.
Details
The stream is an object with five methods:
$headers()returns the initial metadata, a named list.$next_message()returns the next message as a raw vector, orNULLat the end of the stream. It raises a condition, asgrpc_call()does, when the call ends with a status other thanOK. A stream that fails any other way, such as a lost connection, a passed deadline or a broken message, raises the same condition again on every later call. The complete messages that arrived before a broken one are returned first.$trailers()returns the trailing metadata once$next_message()has returnedNULL, andNULLbefore.$status()returns the final status name once the stream has ended, andNULLbefore:"OK", the server's status, or the status the stream failed with, such as"CANCELLED"or"DEADLINE_EXCEEDED". A stream that failed without a status, on a protocol error, staysNULL.$cancel()stops the call. The connection stays open for other calls. A second$cancel()does nothing. A later$next_message()raiseszugrpc_error_cancelled.
A stream that is no longer referenced is cancelled when it is
garbage-collected, and grpc_close() cancels every open stream of its
channel.
Examples
# A test server, from zuhttp's mock transport: it streams two messages
# and ends with status OK.
fake <- zuhttp::zu_mock_transport(function(req) {
zuhttp::zu_response(
200L,
headers = c("content-type" = "application/grpc+proto"),
body = as.raw(c(0, 0, 0, 0, 1, 0x61, 0, 0, 0, 0, 1, 0x62)),
trailers = c("grpc-status" = "0")
)
})
ch <- grpc_channel(
"http://localhost:50051",
client = zuhttp::zu_client(transport = fake)
)
st <- grpc_stream(ch, "/grpc.testing.TestService/StreamingOutputCall", raw())
while (!is.null(m <- st$next_message())) print(rawToChar(m))
#> [1] "a"
#> [1] "b"
st$status()
#> [1] "OK"