S
SolidJS3mo ago
gsoutz

How to crossws websocket Resolver API access getSession?

I am trying to use WebSockets with crossws, I have this piece of code at the beginning:
const ws = crossws({
async resolve(req) {
let user = await getUser()
return {
open(peer) {
let i = dispatch_peer(user, peer)
i?.join()
}
message(peer) {
const ws = crossws({
async resolve(req) {
let user = await getUser()
return {
open(peer) {
let i = dispatch_peer(user, peer)
i?.join()
}
message(peer) {
getUser() uses "use server" and getSession kind of things, so it needs some Context I think. But this getUser() line throws:
file:///workspaces/duckchess25/node_modules/.pnpm/[email protected]/node_modules/unctx/dist/index.mjs:31
throw new Error("Context is not available");
^

Error: Context is not available
at Object.use (file:///workspaces/duckchess25/node_modules/.pnpm/[email protected]/node_modules/unctx/dist/index.mjs:31:15)
at getEvent (/workspaces/duckchess25/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected]/node_modules/vinxi/runtime/http.js:301:33)
at getHTTPEv
file:///workspaces/duckchess25/node_modules/.pnpm/[email protected]/node_modules/unctx/dist/index.mjs:31
throw new Error("Context is not available");
^

Error: Context is not available
at Object.use (file:///workspaces/duckchess25/node_modules/.pnpm/[email protected]/node_modules/unctx/dist/index.mjs:31:15)
at getEvent (/workspaces/duckchess25/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected]/node_modules/vinxi/runtime/http.js:301:33)
at getHTTPEv
I specifically wanted to put the getUser() there, so I don't have to use it inside message(peer), each time message is exchanged getUser is queried. So what is the proper way to have access to user object taken from the session and the database, so it's readily available on websocket handlers?
1 Reply
gsoutz
gsoutzOP3mo ago
In fact, I honestly don't understand how this handler works, I thought async resolve(req), method is only called once when the websocket connection is established, but it is called everytime a message is exchanged? So what is the purpose of a message handler then?
Want results from more Discord servers?
Add your server