Skip to content

SSE ​

Start the stream — real tanstack-fetch/sse against a mock text/event-stream (with Bearer token support).

Live demo · SSE

api.sse over fetch

Real tanstack-fetch/sse against a mock event stream — cookies / Authorization work (unlike EventSource).

Idle — start the stream

import { createFetch } from 'tanstack-fetch/sse'

const api = createFetch({
  plugins: ['sse-resume'],
  getToken: () => localStorage.getItem('access_token'),
})

const stream = api.sse<Tick>('/orders/stream', {
  onMessage: (data) => setTicks((t) => [data, ...t]),
})
// stream.close()

In your app ​

ts
import { createFetch } from 'tanstack-fetch/sse'

const api = createFetch({
  plugins: ['sse-resume'],
  getToken: () => localStorage.getItem('access_token'),
})

const stream = api.sse<OrderEvent>('/orders/stream', {
  onMessage: (data) => console.log(data),
})

stream.close()

Guide: SSE · example: examples/sse-live

MIT Licensed · Created by Mohammad Garmabi · Not an official TanStack package