seven
seven2d ago

can i send data from client to server in subscriptions

Hi, as in the topic, is it possible to do bidirectional websockets transmission, i am trying to build a chat and cannot figure out how to send messages from my nextjs frontend using wsLink to trpc standalone backend (not in nextjs api routes). Many thanks
4 Replies
BeBoRE
BeBoRE23h ago
No they are only for listening, mutations are for sending mutations. The wsLink is bidirectional.
seven
sevenOP16h ago
can you please elaborate? i don't quite get it you said that wsLink is bidirectional but i cannot post any messages via this link, i need to send messages to my ws route in real time, can you please tell how to achieve it?
BeBoRE
BeBoRE14h ago
By using the wsLink to connect to your tRPC backend you can both send and receive information through that WS connection. You request and listen for data through queries and subscriptions, you send data through the use of mutations.
seven
sevenOP4h ago
ok i discussed this with gpt and i think i understand, so mutation on wsLink goes via websocket not via rest, i have this setup hovewer and this is compromising my websocket mutations is that correct? links: [ splitLink({ condition: (op) => op.type === 'subscription', true: wsLink({ client: createWSClient({ url: ws://localhost:7001, }), transformer: superjson, }), false: httpLink({ url: http://localhost:7001, fetch(url, options) { return fetch(url, { ...options, credentials: 'include', cache: 'no-store', }); }, transformer: superjson, }), }), ], so i would have to create two separate clients one for websockets and one for rest is that correct?

Did you find this page helpful?