jstack icon indicating copy to clipboard operation
jstack copied to clipboard

Unable to use websockets

Open Shivam-002 opened this issue 1 year ago • 2 comments

Description

I followed the WebSocket setup as described in the JStack docs, but I encountered the following errors:

  1. async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding 'use client' to a module that was originally written for the server.

  2. A component was suspended by an uncached promise. Creating promises inside a Client Component or hook is not yet supported, except via a Suspense-compatible library or framework.

  3. TypeError: socket.on is not a function

Reproduction Steps

  1. Started from the official JStack WebSocket example.
  2. Implemented the WebSocket connection as shown in the docs.
  3. Ran the project and encountered the above errors.

Shivam-002 avatar Feb 02 '25 16:02 Shivam-002

I've to admin that the documentation could be really improved, but following the example provided on the website you MUST provide a validation schema for incoming and outgoing data, this should solve your problem n.3 .

const chatValidator = z.object({
  message: z.object({
    roomId: z.string(),
    message: z.string(),
    author: z.string(),
  }),
});

chat: j.procedure
    .incoming(chatValidator)
    .outgoing(chatValidator)
    .ws(({ c, io, ctx }) => ({

By doing this my lint becomes happy.

FedericoCasarella avatar Mar 01 '25 18:03 FedericoCasarella

fair, using them without validators should be possible too. if you could pr that would be awesome

joschan21 avatar Apr 08 '25 10:04 joschan21