https://github.com/albert-gao/react-native-fetch-sse
consume text-stream response with standard fetch, works with any custom server and OpenAI API.
https://github.com/albert-gao/react-native-fetch-sse
fetch react react-native sse streaming
Last synced: 6 months ago
JSON representation
consume text-stream response with standard fetch, works with any custom server and OpenAI API.
- Host: GitHub
- URL: https://github.com/albert-gao/react-native-fetch-sse
- Owner: Albert-Gao
- License: mit
- Created: 2023-12-20T23:28:56.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-02T13:13:32.000Z (over 2 years ago)
- Last Synced: 2025-02-01T21:13:04.258Z (over 1 year ago)
- Topics: fetch, react, react-native, sse, streaming
- Language: TypeScript
- Homepage:
- Size: 1.4 MB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# react-native-fetch-sse
consume text-stream response with standard fetch, works with any custom server and OpenAI API.
- Implemented using native `fetch()`, so you can pass headers, different Http methods, `AbortController`, etc.
- minimal implementation, great for web only projects also
- works on React Native / Web
- included all polyfills (only for Native, web doesn't need them)
## Installation
```sh
npm install react-native-fetch-sse
```
## Usage
```js
import { fetchSSE } from 'react-native-fetch-sse';
// in a onPress() handler
const onPress = async () => {
fetchSSE(
'/chat-stream',
{
method: 'POST',
headers: {
Authorization: 'my-token',
},
},
{
onStart() {
console.log('it starts');
},
onMessage(data) {
console.log('receiving event', data);
},
onError(error, { status, statusText }) {
console.log('error', error);
console.log('http status code', status);
console.log('http statusText', statusText);
},
onEnd() {
console.log('the stream finished');
},
// if you are streaming done JSON,
// set this to true will receive the object instead of a string
shouldParseJsonWhenOnMsg: true,
},
);
};
```
## Contributing
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
## Inspired by
- [Azure/fetch-event-source](https://github.com/Azure/fetch-event-source)
- [Vercel/AI](https://github.com/vercel/ai)
## License
MIT
---
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)