Coder Social home page Coder Social logo

trpc-sse-link's Introduction

tRPC x Server-sent-events (SSE)

Supports sending tRPC's subscriptions over SSE.

Note: This is a proof-of-concept and not intended to be used directly.

Components

  • httpSseLink: Client-side tRPC link bridging SSE HTTP connections to the tRPC API
  • Server hack shim: Server-side logic converting subscription procedures to events in a SSE HTTP connection
  • tRPC link split: Client-side configuration sending subscription procedures through httpSseLink

These then underly usage:

  • Server subscription producer

    export const numbers = t.procedure
      .input(
        zod.object({
          count: zod.number(),
        })
      )
      .subscription(async ({ input }) => {
        return observable<{ id: string; idk: number }>((sub) => {
          let i = 0;
          setInterval(() => {
            if (i++ === input.count) {
              sub.complete();
            } else {
              sub.next({
                id: randomUUID(),
                idk: Math.round(Math.random() * 5000),
              });
            }
          }, 1000);
        });
      });
  • Client subscription consumer

    const [messages, setMessages] = useState<Array<{ id: string; idk: number }>>(
      []
    );
    
    trpcClient.numbers.useSubscription(
      {
        max: 25,
      },
      {
        enabled: true,
        onData(data) {
          setMessages((prev) => [...prev, data]);
        },
      }
    );
    
    return (
      <div>
        {messages.map((m) => (
          <span key={m.id}>{m.idk}</span>
        ))}
      </div>
    );

Issues

  • Subscription continues if client closes the connection early
    • I thought this was covered by the server-side request/response event handlers but that seems to be a misunderstanding. See #2.

For the visual people

recording.mp4

trpc-sse-link's People

Contributors

outdatedversion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

xprnio

trpc-sse-link's Issues

Is this still the way to go?

I've notice that the tRPC supports web sockets now, however i don't see any SSE support. Seems like this last update was 10 months ago, but may i ask if this way is still valid?

Thanks!

Amazing work!

I just wanted to leave some positive comments, this looks amazing, great job so far!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.