Redis over HTTP

The same Redis store over fetch, for providers with only a REST API.

server/orders.ts
import { createServer } from 'experimental-a2/server'
import { redisHttp } from 'experimental-a2/store-redis-http'
import { orders } from '@/contracts'

const url = process.env.UPSTASH_REDIS_REST_URL
const token = process.env.UPSTASH_REDIS_REST_TOKEN
if (!url || !token) throw new Error('set the Upstash REST URL and token')

export const ordersServer = createServer({
  contract: orders,
  store: redisHttp({ url, token }),
  handlers: {
    /* ... */
  },
})

Stores sessions exactly like Redis, with no client dependency and nothing to set up. Use it when your provider only offers a REST API.

Options

Option
url, tokenyour provider's REST endpoint and token
keyPrefixisolates several apps on one Redis. Default 'a2'
clock, idstest seams for timestamps and generated event ids

Live updates

  • Live events and presence use Upstash's HTTP SUBSCRIBE endpoint, with the same URL and token. Each active channel holds one outbound HTTP subscription per store instance, shared by its viewers and closed when the last one leaves.
  • Writes publish their wake-ups in the same Redis script as the append. A rejected notification doesn't fail the write.
  • Idle streams re-read every ten seconds to recover a missed notification. Subscriptions reconnect on their own and catch up from the event cursor.
  • After a reconnect, A2 re-reads presence and clears fields deleted during the gap, so presence may repeat its latest values.
  • If SUBSCRIBE answers 404, 405, or 501, the store polls instead, every 25ms while busy and up to every two seconds when idle. Other errors stay errors.