How to bind a new ws endpoint on WebApp with node ws?

will this break meteor default sockjs?

server

import { WebSocketServer } from 'ws'

const wss1 = new WebSocketServer({ noServer: true })

WebApp.httpServer.on('upgrade', function upgrade(request, socket, head) {
  console.log(request.url)
  if (request.url === '/hello') {
    wss1.handleUpgrade(request, socket, head, function (ws) {
      wss1.emit('connection', ws, request)
    })
  }
})

wss1.on('connection', function connection (ws) {
  ws.on('error', console.error)

  ws.on('message', function message(data) {
    console.log('received: %s', data)
  })

  ws.send('something')
})

client

var a = new WebSocket('ws://localhost:3000/hello')
undefined
a.send('message', 'test')
undefined
a.send('message', 'test')
undefined
a.send('message', 'test')
undefined
a.send('message', 'test')

log

=> Meteor server restarted at: http://localhost:3000/
I20230315-15:49:08.002(8)? /sockjs/323/c8ajszl0/websocket
=> Meteor server restarted at: http://localhost:3000/
I20230315-15:51:11.276(8)? /sockjs/787/adit7_8s/websocket
I20230315-15:51:20.704(8)? /sockjs/231/6m2hmrdz/websocket
I20230315-15:51:24.003(8)? /sockjs/270/9i6ebw_9/websocket
I20230315-15:51:24.427(8)? /sockjs/681/o7nrcthx/websocket
=> Meteor server restarted at: http://localhost:3000/
I20230315-15:51:44.781(8)? /sockjs/527/nf8j1bu6/websocket
I20230315-15:51:45.531(8)? /sockjs/419/o2itvu6z/websocket
I20230315-15:51:47.352(8)? /sockjs/546/__3e3ycb/websocket
I20230315-15:52:05.558(8)? /hello
=> Meteor server restarted at: http://localhost:3000/
I20230315-15:52:19.630(8)? /sockjs/438/kui3jq7h/websocket
I20230315-15:52:22.605(8)? /hello
I20230315-15:52:28.789(8)? /hello
=> Meteor server restarted at: http://localhost:3000/
I20230315-15:53:33.921(8)? /sockjs/700/ep9j5m8g/websocket
I20230315-15:53:39.980(8)? /sockjs/625/pwpzsk9t/websocket
I20230315-15:53:41.912(8)? /hello
I20230315-15:53:43.394(8)? received: message
I20230315-15:53:46.656(8)? received: message
I20230315-15:53:48.145(8)? received: message
I20230315-15:53:49.376(8)? received: message
=> Meteor server restarted at: http://localhost:3000/
I20230315-15:54:25.318(8)? /sockjs/877/7ov08x_9/websocket
=> Meteor server restarted at: http://localhost:3000/
I20230315-15:54:26.070(8)? /sockjs/294/xn01jj3_/websocket
1 Like

I never had to do this but as you are using a different endpoint it should be fine.

Curious to understand what is the use case :slight_smile:

I am mainly interested in using the Yjs library to implement collaborative features. Additionally, I would also like to transmit video streams via WebSocket.