wssUpgrade = (req, socket, head) => {
  const streamId = req.url.substring(req.url.lastIndexOf('/') + 1)
  const stream = wssStreams[streamId]
  if (!stream) throw new Error('not allowed')

  const wss1 = new WebSocketServer({ noServer: true })
  wss1.on('connection', async (ws) => {
    stream.events.on('data', async (data) => {
      if (data.err) debug(data)
      data.streamDuration = ((new Date() - stream.dateTimeStart) / 1000).toFixed(3)
      ws.send(JSON.stringify(data))
    })
    stream.events.on('close', () => {
      ws.close()
      wss1.close()
    })
    ws.on('message', (data) => {
      if (Buffer.isBuffer(data)) {
        stream.write(data)
      }
    })
    ws.on('close', () => {
      delete wssStreams[streamId]
      stream.close()
      wss1.close()
    })
    stream.triggerHistoryEmit()
  })
  wss1.handleUpgrade(req, socket, head, (ws) => {
    wss1.emit('connection', ws, req)
  })
}
@swagger
/api/sttend/{streamId}:
  get:
    description: Close a Websocket stream for converting audio stream to text
    security:
      - ApiKeyAuth: []
    produces:
      - application/json
    parameters:
      - name: streamId
        description: Stream Id (as returned from sttstream endpoint)
        in: path
        required: true
        schema:
          type: string
    responses:
      200:
        description: Websocket stream closed