HTTP Subscription Link
httpSubscriptionLink
is a terminating link that's uses Server-sent Events (SSE) for subscriptions.
SSE is a good option for real-time as it's a bit easier than setting up a WebSockets-server.
We have prefixed this as unstable_
as it's a new API, but you're safe to use it! Read more.
Setup
If your client's environment doesn't support EventSource, you need an EventSource polyfill. For React Native specific instructions please defer to the compatibility section.
To use httpSubscriptionLink
, you need to use a splitLink to make it explicit that we want to use SSE for subscriptions.
client/index.tsts
import type { TRPCLink } from '@trpc/client';import {httpBatchLink,loggerLink,splitLink,unstable_httpSubscriptionLink,} from '@trpc/client';const trpcClient = createTRPCClient<AppRouter>({/*** @see https://trpc.io/docs/v11/client/links*/links: [// adds pretty logs to your console in development and logs errors in productionloggerLink(),splitLink({// uses the httpSubscriptionLink for subscriptionscondition: (op) => op.type === 'subscription',true: unstable_httpSubscriptionLink({url: `/api/trpc`,}),false: httpBatchLink({url: `/api/trpc`,}),}),],});
client/index.tsts
import type { TRPCLink } from '@trpc/client';import {httpBatchLink,loggerLink,splitLink,unstable_httpSubscriptionLink,} from '@trpc/client';const trpcClient = createTRPCClient<AppRouter>({/*** @see https://trpc.io/docs/v11/client/links*/links: [// adds pretty logs to your console in development and logs errors in productionloggerLink(),splitLink({// uses the httpSubscriptionLink for subscriptionscondition: (op) => op.type === 'subscription',true: unstable_httpSubscriptionLink({url: `/api/trpc`,}),false: httpBatchLink({url: `/api/trpc`,}),}),],});
The document here outlines the specific details of using httpSubscriptionLink
. For general usage of subscriptions, see our subscriptions guide.
Headers and authorization / authentication
Web apps
Same domain
If you're doing a web application, cookies are sent as part of the request as long as your client is on the same domain as the server.