Skip to content

Cannot read properties of undefined ( ws + nodejs ) #981

@ruspaul013

Description

@ruspaul013

Hello! Is there any support for ws planned?

I'm trying to use unit with ws and node loader in a demo app, but I still getting this error:

/test/node_modules/ws/lib/websocket.js:231
    if (head.length > 0) socket.unshift(head);
             ^

TypeError: Cannot read properties of undefined (reading 'length')
    at WebSocket.setSocket (/test/node_modules/ws/lib/websocket.js:231:14)
    at WebSocketServer.completeUpgrade (/test/node_modules/ws/lib/websocket-server.js:411:8)
    at WebSocketServer.handleUpgrade (/test/node_modules/ws/lib/websocket-server.js:336:10)
    at Server.upgrade (/test/node_modules/ws/lib/websocket-server.js:112:16)
    at Server.emit (node:events:513:28)
    at Server.emit_request (/usr/lib/node_modules/unit-http/http_server.js:485:14)

Demo app

index_ws.js file:

const { WebSocketServer } = require('ws')
const sockserver = new WebSocketServer({ port: 3010 })
sockserver.on('connection', ws => {
         console.log('New client connected!')
         ws.send('connection established')
         ws.on('close', () => console.log('Client has disconnected!'))
         ws.on('message', data => {
                    sockserver.clients.forEach(client => {
                                 console.log(`distributing message: ${data}`)
                                 client.send(`${data}`)
                               })
                  })
         ws.onerror = function () {
                    console.log('websocket error')
                  }
})

Config file

Unit config file:

{
    "listeners": {
        "*:9096": {
            "pass": "routes"
        }
    },
    "routes": [
        {   "match": {
                "uri": [
                    "/ws1"
                ]
            },
            "action": {
                "rewrite": "/",
                "pass": "applications/node-test-ws-1"
            }
        }
    ],
    "applications": {
        "node-test-ws-1": {
            "user": "node",
            "group": "node",
            "type": "external",
            "working_directory": "/test/ws",
            "executable": "/usr/bin/node",
            "arguments": [
                "--loader",
                "unit-http/loader.mjs",
                "--require",
                "unit-http/loader",
                "index_ws.js"
            ]
        }
    }
}

Application with node it's working.

Environment

OS:

AlmaLinux release 8.8 (Sapphire Caracal)

Unit version:

unit version: 1.31.1
configured as ./configure --prefix=/usr/ --state=/var/lib/unit --control=unix:/var/run/control.unit.sock --pid=/var/run/unit.pid --log=/var/log/unit.log --tmp=/var/tmp --user=unit --group=unit --tests --openssl --modules=/usr/lib/unit/modules --libdir=/usr/lib/

unit-http build local:

./configure nodejs --node=/usr/bin/node --npm=/usr/bin/npm --node-gyp=/usr/bin/node-gyp

Metadata

Metadata

Assignees

Labels

z-documentation-update-neededThis type of issues likely have a link to an issue in the Unit Docs repo

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions