Skip to content

useXStream ๐ŸŒฑ โ€‹

Introduction โ€‹

This hook function allows users to more conveniently control streaming requests. It provides initiate request, cancel request, returns loading request state, returns SSE protocol real-time data stream, and returns request error information.

๐Ÿ’Œ Info

Currently only SSE and SIP protocol requests have been tested, other protocols are yet to be tested. If you have good ideas or discoveries, you are welcome to join the community group ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง Community Group, contact us, share solutions, submit issues and PRs. Please read the ๐Ÿ‘‰ Development Guide for submission specifications.

Code Examples โ€‹

This hook function is aligned with ant-design-x's XStream method, and we have integrated the Vue development paradigm.

On this basis, we have added interrupt handling for streaming requests, and placed the original hook function's configuration method in the startStream parameter, making it easier for developers to understand the purpose of this hook.

This demonstrates support for the SIP protocol.

๐Ÿ“Œ Warning

The parsing rules of this hook are consistent with ant-design-x, all handled internally. Feel free to switch and use

sseEventPart 'event: message\ndata: {"id":"${i}","content":"${contentChunks[i]}"}\n\n'

ts
// Default stream separator (uses two newlines to split a stream data)
const DEFAULT_STREAM_SEPARATOR = '\n\n';
// Default part separator (uses a single newline to break current data)
const DEFAULT_PART_SEPARATOR = '\n';
// Default key-value separator (uses colon)
const DEFAULT_KV_SEPARATOR = ':';

Returned Hooks โ€‹

PropertyDescriptionType
startStreamStart request in stream mode({readableStream, transformStream}) => void
cancelCancel streaming request() => void
loadingIs streaming data loadingboolean
dataReal-time streaming datastring
errorStreaming request error infostring