How to add something to Companion that ignores websocket connections that are not intended for Companion

I am setting up both a socket.io server and Companion’s uppy.socket() on the same http server instance.

Companion doesn’t use socket.io so i can’t use socket.io-client to connect to it. If both servers are set up on the same http.Server instance, they may both receive incoming WebSocket connections. In that case i should add something to Companion that ignores connections that are not intended for Companion.

and how do i add something to Companion that ignores connections that are not intended for Companion ?

server.js

const companion = require("@uppy/companion")
const express = require("express")
const { createServer } = require("http")
const { Server } = require("socket.io")

const app = express()
const httpServer = createServer(app)
const io = new Server(httpServer, {
serveClient: false
})
app.use("/companion", companion.app(options))
httpServer.listen(3000)

companion.socket(httpServer)

this giving me error:
companion: 2022-12-01T21:47:09.881Z [info] socket.connect connection received
from /socket.io/?EIO=4&transport=websocket&sid=LdGVCwuehiB4bzJAAAAA
node:events:368
throw er; // Unhandled ‘error’ event
^

Error: Invalid WebSocket frame: invalid UTF-8 sequence

Hi, I’m a bit surprised Companion sends this error unless your client also sends to /companion. Companion only has one socket endpoint: /api/:id (in your case /companion/api/:id) so it’s interesting that /socket.io end up in the Companion route. Could this be a client-side misconfig?

thank you for your reply.
client.js

const { io } = require("socket.io-client")
const socket = io()
socket.on("connect", () => {
  console.log("client connected")
})
socket.on("connect_error", () => {
  console.log("client connection error")
})
socket.on("disconnect", reason => {
  console.log("client disconnect:", reason)
})

this is client-side config.

Error
companion: 2022-12-03T15:18:14.739Z [info] socket.connect connection received
from /socket.io/?EIO=4&transport=websocket&sid=8Xd28QHOba1iFIZnAAAA
node:events:368
throw er; // Unhandled ‘error’ event
^

Error: Invalid WebSocket frame: invalid UTF-8 sequence
at Receiver.dataMessage (C:\Users\rithi\OneDrive\Desktop\KhabarBook\backend\node_modules\ws\lib\receiver.js:519:18)
at Receiver.getData (C:\Users\rithi\OneDrive\Desktop\KhabarBook\backend\node_modules\ws\lib\receiver.js:446:17)
at Receiver.startLoop (C:\Users\rithi\OneDrive\Desktop\KhabarBook\backend\node_modules\ws\lib\receiver.js:148:22)
at Receiver._write (C:\Users\rithi\OneDrive\Desktop\KhabarBook\backend\node_modules\ws\lib\receiver.js:83:10)
at writeOrBuffer (node:internal/streams/writable:389:12)
at _write (node:internal/streams/writable:330:10)
at Receiver.Writable.write (node:internal/streams/writable:334:10)
at Socket.socketOnData (C:\Users\rithi\OneDrive\Desktop\KhabarBook\backend\node_modules\ws\lib\websocket.js:1272:35)
at Socket.emit (node:events:402:35)
at Socket.emit (node:domain:475:12)
Emitted ‘error’ event on WebSocket instance at:
at Receiver.receiverOnError (C:\Users\rithi\OneDrive\Desktop\KhabarBook\backend\node_modules\ws\lib\websocket.js:1158:13)
at Receiver.emit (node:events:390:28)
at Receiver.emit (node:domain:475:12)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: ‘WS_ERR_INVALID_UTF8’,
[Symbol(status-code)]: 1007
}