SolidJSS
SolidJSโ€ข17mo ago
Khoi

Solid-start/sockets only in development mode

Hi everyone!

I'm using Solid-Start and am creating an API route with the following code:

import type { APIEvent } from '@solidjs/start/server'
import { WebSocketServer } from 'ws'

const wss = new WebSocketServer({ noServer: true })
wss.on('connection', (ws) => {
  ws.on('message', (message) => {
    wss.clients.forEach((client) => {
      client.send(String(message))
    })
  })
})

export function GET(event: APIEvent) {
  const request = event.nativeEvent.node.req
  wss.handleUpgrade(request, request.socket, Buffer.from(''), (ws) => {
    wss.emit('connection', ws, request)
  })
}

It works in development mode (npm run dev), but when I do npm run build && npm run start, I can't connect to the socket server. For example, Chrome gives me:

WebSocket connection to 'ws://localhost:3000/api/boards' failed: Invalid frame header

Can you help me?

EDIT: I've added

    experimental: {
      websocket: true,
    },


to my nitroconfig and the error disappears. However, the messages don't seem to be received by the server
Was this page helpful?