kibana/examples/response_stream/README.md
Walter Rafelsberger 5345e34ddc
[ML] Adds redux toolkit example for response_stream to developer examples. (#182690)
## Summary

Follow up to #132590.
Part of #181111.

This updates the developer examples for `@kbn/ml-response-stream` to
include a variant with a full Redux Toolkit setup. For this case, the
`@kbn/ml-response-stream` now includes a generic slice `streamSlice`
that can be used. This allows the actions created to be streamed via
NDJSON to be shared across server and client.

Functional tests for the examples were added too. To run these tests you
can use the following commands:

```
# Start the test server (can continue running)
node scripts/functional_tests_server.js --config test/examples/config.js
# Start a test run
node scripts/functional_test_runner.js --config test/examples/config.js
```

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
2024-05-22 16:51:36 +02:00

1.9 KiB

response stream

This plugin demonstrates how to stream chunks of data to the client with just a single request.

To run Kibana with the described examples, use yarn start --run-examples.

The response_stream plugin demonstrates API endpoints that can stream data chunks with a single request with gzip/compression support. gzip-streams get decompressed natively by browsers. The plugin demonstrates some use cases to get you started:

  • Streaming just a raw string.
  • Streaming NDJSON with "old-school" redux like actions and client side state managed with useFetchStream(). This uses React's own useReducer() under the hood.
  • Streaming NDJSON with actions created via Redux Toolkit's createSlice() to a client with a full Redux Toolkit setup.

Code in @kbn/ml-response-stream contains helpers to set up a stream on the server side (streamFactory()) and consume it on the client side via a custom hook (useFetchStream()) or slice (streamSlice()). The utilities make use of TS generics in a way that allows to have type safety for both request related options as well as the returned data.

Besides Redux Toolkit for its particular use case, no additional third party libraries are used in the helpers to make it work. On the server, they integrate with Hapi and use node's own gzip. On the client, the custom hook abstracts away the necessary logic to consume the stream, internally it makes use of a generator function and useReducer() to update React state.

On the server, the simpler stream to send a string is set up like this:

const { end, push, responseWithHeaders } = streamFactory(request.headers);

The request's headers get passed on to automatically identify if compression is supported by the client.

On the client, the custom hook is used like this:

const { errors, start, cancel, data, isRunning } = useFetchStream(
  '/internal/response_stream/simple_string_stream'
);