Skip to content

Websocket subscription optionally accepts input payload as function and re-evaluates on reconnect (#1574) - #1584

Closed
jlalmes wants to merge 5 commits into
mainfrom
feature/issue-1574
Closed

jlalmes wants to merge 5 commits into
mainfrom
feature/issue-1574

Conversation

@jlalmes

@jlalmes jlalmes commented Mar 1, 2022 •

Copy link
Copy Markdown
Contributor

This PR implements a backwards compatible way to optionally specify the subscription input payload as a function. The input value is resolved by this function each time the socket reconnects.

client.subscription('eventName', input, { ... })
// OR (now possible with this PR)
client.subscription('eventName', () => input, { ... })

These changes do not affect the useSubscription hook in @trpc/react, only the raw TRPCClient from @trpc/client.

Closes #1574

@vercel

vercel Bot commented Mar 1, 2022 •

Copy link
Copy Markdown

@jlalmes

jlalmes commented Mar 1, 2022

Copy link
Copy Markdown
Contributor Author

It seems that one of the build jobs is timing out and therefore failing - Could you take a look @KATT ?
https://github.com/trpc/trpc/runs/5380102303

@jlalmes

jlalmes commented Mar 2, 2022

Copy link
Copy Markdown
Contributor Author

Just re-run the tests looks like it didn't timeout this time. Awaiting your feedback @KATT 👍

@KATT

KATT commented Mar 6, 2022

Copy link
Copy Markdown
Member

I still really struggle to see how this should be a client-side concern to keep track of? 🤔

@KATT

KATT commented Mar 7, 2022

Copy link
Copy Markdown
Member

I really appreciate you're doing this.

Maybe you can share more details of your use-case @jlalmes, and why this isn't something the server can pick up on it's own?

To me, it looks like something that the server should "know" after the createContext is called, which happens on first connect for WebSockets too.

@jlalmes

jlalmes commented Mar 7, 2022 •

Copy link
Copy Markdown
Contributor Author

Hi @KATT, thanks for getting back to me.

Use case - I am working on an internal web analytics tool:

  • Client visits webpage for first time
  • Client subscribes to server (via trpc) input: { sessionId: null }
  • Server creates a session in the DB and returns the sessionId to the client
  • Client saves this sessionId in sessionStorage
  • Client navigates to a different page (causing page reload)
  • Client subscribes to server input: { sessionId: "abc123" }

Reason for this PR

  • If a client subscribes with input: { sessionId: null }
  • A new session will be created on the server
  • Then the client disconnects from the server due to bad network conditions
  • When the client auto-reconnects (network connection reestablished) it will send input: { sessionId: null } again
  • Another session will be needlessly created on the server

Why I propose this API

Current workaround

const subscribe = () => {
  const unsubscribe = trpcWsClient.subscription('session', { sessionId: window.sessionStorage.getItem('session') }, {
    onNext: (result) => {
      if (result.type === 'data') {
        window.sessionStorage.setItem('session', result.data.sessionId);
      }
    },
    onError: (error) => {
      console.error(error);
      unsubscribe();
      subscribe();
    },
  });
};

subscribe();

Usage after this PR

trpcWsClient.subscription('session', () => ({ sessionId: window.sessionStorage.getItem('session') }), {
  onNext: (result) => {
    if (result.type === 'data') {
      window.sessionStorage.setItem('session', result.data.sessionId);
    }
  }
});

Let me know if this is not clear.

Thanks

@KATT

KATT commented Mar 7, 2022

Copy link
Copy Markdown
Member

Hmmm.. is there a reason why you aren't using a session cookie? Cookies are usually the way to deal with this on the web

@jlalmes

jlalmes commented Mar 10, 2022

Copy link
Copy Markdown
Contributor Author

Fair point I suppose, closing this PR & issue for now.

@jlalmes jlalmes closed this Mar 10, 2022
auto-merge was automatically disabled March 10, 2022 12:13

Pull request was closed

@KATT

KATT commented Mar 10, 2022

Copy link
Copy Markdown
Member

I really appreciate the work you did on this @jlalmes, feel free to reopen a discussion about it if you feel it's something we should support!

@github-actions

github-actions Bot commented Oct 7, 2022

Copy link
Copy Markdown
Contributor

This pull request has been locked because it had no new activity for 30 days. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 7, 2022
@KATT
KATT deleted the feature/issue-1574 branch November 22, 2022 09:48

This branch was successfully deployed

3 active deployments
Preview – www — 58375e0b Deployed Mar 1, 2022 by vercel[bot]
Preview – todomvc — 58375e0b Deployed Mar 1, 2022 by vercel[bot]
Preview – next-prisma-starter — 58375e0b Deployed Mar 1, 2022 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reevaluate WebSocket Subscription Input Payload on Reconnection

2 participants