2 years ago

#9619

test-img

cicciosgamino

How find into Koa context the WebSocket and Request connected to a connection?

I'd like connect the Koa application used mainly for easy handling of the middlewares, the { createServer } from http for handling the http server events, and the { WebSocketServer } from ws to handling the WebSocket connection and WebSocketServer events ... all connected as:

const app = new Koa()
const server = createServer(app.callback())
const wss = new WebSocketServer({server})

I'm using the Router middleware too for serve the different path and methods. But my question is how i can get the Koa context for a WebSocket connection (ws, req):

 wss.on('connection', async (ws, req) => { // Koa ctx ?? })

Or how can i get the WebSocket object into a Koa middleware (eg. a route):

router.get('/get', async (ctx, next) => { // WebSocket ?? })

Or inside a general Koa middleware:

app.use(async (ctx, next) => { // WebSocket ??})

I'd like to understand how the wss (WebSocketServer) is connected with app (Koa Application) through the http server!

node.js

websocket

koa

ws

0 Answers

Your Answer

Accepted video resources