next-prisma-websockets-starter explanation
This is the repo I'm trying to copy websocket setup from... https://github.com/trpc/examples-next-prisma-websockets-starter
Inside of the server folder in this template, [https://github.com/trpc/examples-next-prisma-websockets-starter/tree/main/src/server] there is a
prodServer.ts
file and a wssDevServer.ts
file. How are these files used?? When and how is the prodServer.ts
file used(ran) and how is the wssDevServer.ts
file ran?
Because in the trpc.ts
file [https://github.com/trpc/examples-next-prisma-websockets-starter/blob/main/src/utils/trpc.ts] the wsLink
is used but there are no checks for dev or prod. And WS_URL
is imported from publicRuntimeConfig
. This is confusing.
The tRPC docs also do it differently. They hardcode the websocket urls. And there are no checks for dev and prod.GitHub
GitHub - trpc/examples-next-prisma-websockets-starter: 🏓 tRPC Next....
🏓 tRPC Next.js WebSocket Starter. Contribute to trpc/examples-next-prisma-websockets-starter development by creating an account on GitHub.
38 Replies
This is what my
trpc.ts
file in the utils folder looks like
I want to add my websocket url to this. But the template from github does it differently. the docs also do it differently. Web dev simplified on Youtube also has a different setup.
This is my wssServer.ts
file
tbh I don't know how this is gonna run. It has no exports.Quickly looking at the repo, they seem to simply be called from scripts in package.json,
start
and dev:wss
,
For npm run dev
, it excecutes pnpm migrate-dev && run-p dev:*
which means it is running both dev:next
and dev:wss
run-p
is just a package that lets you run package.json scripts in paralleloww
the tRPC don't show the package.json setup
I don't think that would work for me tho
Cuz it's for pnpm
man
this is a headache.
i did this by hosting a websocket server on railway and a http server on vercel and then connecting the two using redis pub/sub
i just put the websocket server in a sep pkg in my monorepo
I just got this working for localhost
I'm not using a monorepo tho
wow,
ya but u can just have another ts file and run it concurrently
And the same one can be used for locahost and prod right?
The ports were different
Ya
okay
I have a separate ws file for prod and localhost
this is the production one
r u trying to connect this to the client?
I didn't set notifications to all messages
Yes
In here
the example didn't work for me so i split the link
where is your websocket url
ah yes I saw WebDevSimplified doing this
how not
i dont remember i just remember doing split link cuz i was getting errors
lemme send code for that
alr
and the getBaseUrl function?
I wanna see how you check
o it just returns the url to the host:
for my case i did diff ports but u can do same port
ahh cool
your websocket s always running on railway?
didn't know you could check for localhost like that
ya i got that from the
create-t3-turbo
example
yepwhere is your local websocket running?
like mine?
kewl
on local it shud be
localhost:3000
for both cuz ur using same port i thinkMine's running like this. On port 3001
it currently works
when is start dev, my ws also starts:
o shoot i misread mb
i thought it was same port
np 😉
currently my prod one isnt working
it fails when I build and run start
wats the log
Are you importing
z
from zod
at the top of your env.mjs
?
yes
idk then, i think this is some sort of ts issue
it prolly has todo with this script
this is my
tsconfig.server.json
file
And i'm not sure what i'm doing wrong or missing here.My tsconfig compiler options is the following
maybe try modifying some of the options
there are two tsconfig files
when I delete the dist folder and run build again, I don't get the error.
If I run the build script again, the error then pops up.
Not sure what the cause of that is
I get a different error.
these are all import errord
like
@saiansh2525 may I see your package.json?
But where is
Constants
imported from?that was just something specific to expo
@kenny were you able to get your app working with websockets? If possible would you be able to share your repo where you got websockets to work with the t3 create app?
I'm trying to do a similar thing where i integrate the prisma repos implementation of websockets into my t3 create app
Hi
I still have the repo yes. But I didn't push the changes live iirc cause the web socket functionality wasn't working correctly on a live server.
I think the fix was just not to use a local ws url 😅 idk maybe you can figure that out.
Here's the repo https://github.com/Beefy-py/chat-app.git.
You'd want to check the added websocket functionality commit
pls ignore the
README.md
🙊okay thanks i'll give it a shot
I found this guide for how to get websockets to work but its pretty hard for me to navigate and understand the project structre of t3 as a beginner: https://medium.com/@lada496/trpc-102-subscription-web-socket-d81b8962a010
Medium
tRPC 102: Subscription/Web Socket
This is my memo when I implemented a subscription with tRPC along with the official documents. I also referred to the code example provided…