Skip to content

feat: support using fetchEventSource for subscriptions #6950

Open

Description

@goleary

Describe the feature you'd like to request

EventSource is lacking a ton of functionality that makes auth a giant PITA

  • headers are not supported
  • cross origin cookies are not supported.

Describe the solution you'd like to see

fetchEventSource fixes these shortcomings of EventSource.

Would be awesome to be able to utilize this out of the box in tRPC.

Describe alternate solutions

We use tRPC with header auth, but now to use subscriptions we have to either expose our tokens via a get param or implement a subscription specific token process.

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR implementing this feature!

Activity

  1. KATT commented on Sep 18, 2025

    @KATT
    Member

    Yeah, I want to get rid of EventSource too. I agree with everything

  2. added
    ✅ accepted-PRs-welcome
    on Sep 18, 2025
  3. KATT commented on Sep 18, 2025

    @KATT
    Member

    I'd do a new httpLink, make sure it works with resolveResponse, and deprecate the old one (ensuring that there's functionality pairity)

  4. ravikant-diwakar commented on Sep 19, 2025

    @ravikant-diwakar

    Technical Implementation Approach

    This is an excellent enhancement for tRPC's subscription architecture. Here's my proposed technical solution:

    Core Implementation Strategy

    1. New HTTP Link Architecture

      • Create httpFetchEventSourceLink alongside existing httpSubscriptionLink
      • Implement as a drop-in replacement with enhanced auth capabilities
      • Maintain backward compatibility with existing resolveResponse pipeline
    2. TypeScript Interface Design

    interface FetchEventSourceLinkOptions<TRouter extends AnyRouter = AnyRouter> 
      extends HTTPLinkOptions<TRouter> {
      fetchEventSource?: typeof fetchEventSource;
      headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
      credentials?: RequestCredentials;
      onRetry?: (err: any, retryCount: number) => number | false;
    }
    1. Authentication Flow Enhancement

      • Support dynamic header injection for JWT/Bearer tokens
      • Enable cookie-based authentication for cross-origin scenarios
      • Implement retry logic with token refresh capability
    2. Migration Path

      • Deprecate EventSource-based implementation with clear migration guide
      • Provide feature parity testing suite
      • Add comprehensive TypeScript types for new subscription client

    Key Benefits

    • Solves header-based auth limitations
    • Enables cross-origin cookie support
    • Maintains type safety throughout subscription lifecycle
    • Provides better error handling and retry mechanisms

    Would be happy to contribute to the implementation if this approach aligns with the team's vision. The complexity involves deep integration with tRPC's link system and subscription resolver pipeline.

  5. KATT commented on Sep 19, 2025

    @KATT
    Member

    @ravikant-diwakar if you gonna have ai write for you, tell it to be concise. I've already said that I accept prs and the constraints in one sentence. If you have actual questions once/if you start work on it, please let me know

  6. Kkartik14 commented on Jan 11, 2026

    @Kkartik14

    Hey @KATT we can use fetch and manual SSE parsing to update headers, If it's still open, can i work on it?

  7. evolvomind commented on Feb 19, 2026

    @evolvomind
    Contributor

    Hi, @KATT
    May I work on this issue?

  8. nylakaidan-ship-it commented on Mar 29, 2026

    @nylakaidan-ship-it
  9. KATT commented on Nov 2, 2026

    @KATT
    Member

    🤖 In v12, httpLink reads subscriptions as SSE over fetch instead of EventSource, so the link's headers and init options (e.g. credentials for cross-origin cookies) apply to subscriptions just like queries and mutations. https://v12.trpc.io/v12/docs/client/links/http#subscriptions. v12 isn't released yet and there's no ETA.

  10. added
    next-major/definite
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-major/definite✅ accepted-PRs-welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions