Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api/socketio.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ app.configure(
app.listen(3030)
```

When the server passed to `app.setup(server)` is itself built on uWS and exposes its app as `server.uwsApp`, Socket.io is attached to it with `io.attachApp` automatically.

### socketio(options [, callback])

`app.configure(socketio(options [, callback]))` sets up the Socket.io transport with the given [Socket.io options object](https://github.com/socketio/engine.io#methods-1) and optionally calls the callback described above.
Expand Down
8 changes: 7 additions & 1 deletion packages/socketio/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ function configureSocketio(port?: any, options?: any, config?: any) {

async setup(this: any, server: http.Server, ...rest: any[]) {
if (!this.io) {
const io = (this.io = new Server(port || server, options))
// A server built on uWebSockets.js exposes its app as `uwsApp`, socket.io attaches to it natively
const uwsApp = !port && server && 'uwsApp' in server ? (server as any).uwsApp : null
const io = (this.io = uwsApp ? new Server(options) : new Server(port || server, options))

if (uwsApp) {
io.attachApp(uwsApp)
}

io.use(disconnect(app, getParams, socketMap))
io.use(params(app, socketMap))
Expand Down
21 changes: 21 additions & 0 deletions packages/socketio/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,27 @@ describe('@feathersjs/socketio', () => {
})
})

it('attaches to a server built on uWebSockets.js', async () => {
// a stand-in for a uWebSockets.js app, recording the routes socket.io registers
const routes: string[] = []
const uwsApp: any = {}
for (const method of ['any', 'get', 'ws']) {
uwsApp[method] = (path: string) => {
routes.push(`${method} ${path}`)
return uwsApp
}
}
// such servers are callable request handlers, like an Express app
const server: any = Object.assign(() => {}, { uwsApp })
const application = feathers().configure(socketio({ path: '/ws/' }))

await application.setup(server)
// attachApp patches the socket.io adapter globally, close() restores it for the other tests
await application.io.close()

assert.ok(routes.includes('ws /ws/'))
})

it('passes handshake as service parameters', (done) => {
socket.emit('create', 'verify', {}, (error: any, data: any) => {
assert.ok(!error)
Expand Down