Hopefully someone can help me out here, I’ve been trying to figure out where the problem is getting a nodejs server app to connect to HA as a websocket client. I’ve tried various libraries and different connection methods however the connection is always closed. I’m starting to wonder if it’s HA’s implementation of the websocket API. Also version 0.33 behaves differently than HA version 0.34 ( up to date tested on 0.40.2).
You can see in the below output that HA version 34 and 40 both receive the message sent and accept the auth msg with api_password, however after that the connection is closed and also I have to do this blindly instead of a handler for .on('message', ...
as regardless of the version the message handler never gets invoked so I cannot switch on the message type. HA Version 0.33 throws a 404, I’m not sure it’s relevant, could be an implementation change but thought I’d include to be thorough.
I realize this that this sounds like it could be a problem with the websocket
library in node, maybe it is, but hopefully someone has some tips here because I’m gonna go blind staring at this any longer ;o) I’ve included a docker-compose file to setup ha with the version wanted to test along with the config and the nodejs script, hopefully that will help a bit.
Steps to reproduce
- Create the files with contents in this post
- Create directory structure
<root-folder>/homeassistant-docker/config
- Paste
configuration.yaml
into<root-folder>/homeassistant-docker/config/configuration.yaml
- Create
<root-folder>/docker-compose.yaml
- Create
<root-folder>/websocket.js
- cd to the
<root-folder>
rundocker-compose up -d && docker-compose -f --tail
- in another terminal cd to
<root-folder>
and runnode websocket.js
Files mentioned in above steps
websocket.js (run npm install websocket
first)
const util = require('util');
var client = new (require('websocket').client)();
client.on('connectFailed', (err) => console.log('**connectFailed: ', util.inspect(err)));
client.on('connect', function(conn) {
console.log('**onConnect');
conn.on('error', (error) => console.log('**onError: ', util.inspect(err)));
conn.on('close', () => console.log('**onClose'));
conn.on('message', (msg) => util.inspect(msg));
const authObj = { type: 'auth', api_password: 'testing123' };
conn.send(JSON.stringify(authObj));
});
client.connect('ws://localhost:8123/api/websocket');
configuration.yml
homeassistant:
name: Home
unit_system: metric
time_zone: America/Los_Angeles
frontend:
http:
api_password: !env_var API_PASS
logger:
default: debug
docker-compose.yml
version: "3.1"
services:
hass:
image: homeassistant/home-assistant:0.40.2
#image: homeassistant/home-assistant:0.34.0
#image: homeassistant/home-assistant:0.33.0
environment:
API_PASS: testing123
ports:
- '8300:8300'
- '8123:8123'
volumes:
- ./homeassistant-docker/config:/config
- /etc/localtime:/etc/localtime:ro
Output
####0.40.2 (Same as 0.34.0)
- From HA logs
hass_1 | 17-03-22 19:07:11 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 139898631535360: Connected
hass_1 | 17-03-22 19:07:11 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 139898631535360: Sending {'ha_version': '0.40.2', 'type': 'auth_required'}
hass_1 | 17-03-22 19:07:11 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 139898631535360: Sending {'ha_version': '0.40.2', 'type': 'auth_ok'}
hass_1 | 17-03-22 19:07:11 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 139898631535360: Connection closed by client
hass_1 | 17-03-22 19:07:11 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 139898631535360: Closed connection
- From Node:
⟴ node websocket.js
**onConnect
**onClose
####0.34.0
- From HA logs
hass_1 | 17-03-22 15:12:51 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 139942411936768: Connected
hass_1 | 17-03-22 15:12:51 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 139942411936768: Sending {'ha_version': '0.35.0', 'type': 'auth_required'}
hass_1 | 17-03-22 15:12:51 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 139942411936768: Sending {'ha_version': '0.35.0', 'type': 'auth_ok'}
hass_1 | 17-03-22 15:12:51 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 139942411936768: Connection closed by client
hass_1 | 17-03-22 15:12:51 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 139942411936768: Closed connection
- From Node:
⟴ node websocket.js
**onConnect
**onClose
####0.33
- Nothing from HA logs
- From running node below:
⟴ node websocket.js
**connectFailed: Error: Server responded with a non-101 status: 404
Response Headers Follow:
content-type: text/plain; charset=utf-8
content-length: 14
date: Wed, 22 Mar 2017 19:02:42 GMT
server: Python/3.5 aiohttp/1.0.5
at WebSocketClient.failHandshake (.../homeassistant-tests/ha-ws/node_modules/websocket/lib/WebSocketClient.js:326:32)
at ClientRequest.<anonymous> (.../homeassistant-tests/ha-ws/node_modules/websocket/lib/WebSocketClient.js:265:18)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:189:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:522:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at Socket.socketOnData (_http_client.js:411:20)
at emitOne (events.js:96:13)
at Socket.emit (events.js:189:7)
at readableAddChunk (_stream_readable.js:176:18)