Exploring Home Assistant with Node.js and Reverse Proxy

Hey everyone, I’m really excited to share my recent project with you! I’ve been diving into the world of Home Assistant and Node.js, and I wanted to document my journey in case anyone else is interested in setting up a similar system. :rocket:

So, here’s the setup: I wanted to create a reverse proxy using Node.js and Express to connect my Home Assistant instance from an external location. This involved setting up a DNS name, in this case, lavamotion.net, and using a Let’s Encrypt certificate for secure communication. The idea was to have one DNS name with different ports handling HTTP, HTTPS, and WebSocket connections.

I started by installing the necessary modules like express, http-proxy-middleware, and fs for handling file operations. The first step was configuring the HTTPS server with the Let’s Encrypt certificate. I made sure to enable trust proxy and set up a redirect from HTTP to HTTPS to ensure all traffic was secure.

One of the challenges I faced was setting up the WebSocket proxy for Home Assistant. I used createProxyMiddleware from http-proxy-middleware to target the internal Home Assistant server running on port 8123. This allowed me to connect to Home Assistant via HTTPS on port 8123 without any internal certificates.

Another important aspect was ensuring the Let’s Encrypt certificate renewals worked seamlessly. I added a static route to serve the challenge files under .well-known/acme-challenge/, which is necessary for the certificate renewal process.

Throughout the process, I kept testing each component to make sure everything was working as expected. The HTTP server on port 80 redirects to HTTPS, the HTTPS server handles the main routes, and the WebSocket server manages the connection to Home Assistant.

I also set up a simple /info endpoint to verify that the server was running correctly. This was a quick way to ensure that the certificate was properly configured and that the server was accessible over HTTPS.

One thing I learned along the way is the importance of handling errors gracefully. I made sure to include proper error handling for the WebSocket connections and set up logging to monitor the server’s performance.

Overall, this project has been a fantastic learning experience. It’s amazing how Node.js and Express can be used to create such a robust and flexible system. I’m now able to access my Home Assistant instance securely from anywhere, which is incredibly convenient.

If anyone has questions or suggestions, I’d love to hear them! Let’s continue to explore and innovate together. :star2: