tinyhttp / tinyws

🚡 tiny WebSocket middleware for Node.js
https://npm.im/tinyws
MIT License
407 stars 15 forks source link

How to get websocket server access? #6

Closed reesericci closed 2 years ago

reesericci commented 2 years ago

Is there a method for accessing the websocket server?

talentlessguy commented 2 years ago
import { Server } from 'ws'
import { tinyws } from 'tinyws'
import { App } from '@tinyhttp/app'

const app = new App()
const wss = new Server({ noServer: true })

app.use(tinyws(opts, wss))
talentlessguy commented 2 years ago

https://github.com/tinyhttp/tinyws/blob/e91c40cda7ef91a6bf6c08d6742531bcd94d1d39/tests/index.test.ts#L116-L132

go4cas commented 2 years ago

@talentlessguy ... I have implemented as you recommended, but I am not sure how to access thewss instance from within a handler? Any suggestions?

talentlessguy commented 2 years ago

@go4cas wss here has a global scope so you can just use it within a handler as any other variable

app.use((req, res) => {
//  wss.something
})