I'm using a websocket that changes it's URL frequently, is it possible to workaround this (for example pass options via a msg to configure the URL?).
Currently I'm using: https://flows.nodered.org/node/node-red-contrib-function-npm and manually implementing ws via 'require', but this isn't flexible as once a websocket connection is opened, I can't seem to close it, even by redeploying the function node (I guess it runs as another process in the background!).
Sadly, the endpoint does change (like one of IBM's own Watson endpoints!).
Further, at times I find myself needing to dynamically create endpoint URL's due to changing product options, I was hoping there was a way I could automate endpoint changes...
I ended up solving the URL issue with NGINX, here is my config:
server {
listen 80;
server_name mqtt.url;
client_max_body_size 0;
root /config/www/php/;
location / {
fastcgi_index ws.php;
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
}
}
server {
listen 80;
listen 443 ssl;
server_name proxy; # could be a full URL in case this is needed
client_max_body_size 0;
# all ssl related config moved to ssl.conf (docker)
include /config/nginx/ssl.conf;
location / {
proxy_pass http://localhost/ws.php;
proxy_set_header Host "mqtt.url"; # redirect to earlier instance
proxy_intercept_errors on;
error_page 301 302 307 = @handle_redirect; # redirect internally
}
location @handle_redirect {
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "upgrade";
set $saved_redirect_location '$upstream_http_location'; # This will have the new URL
proxy_pass $saved_redirect_location;
}
}
The job of determining which URL to redirect to is handled, in this case, by PHP:
<?php
$url = file_get_contents("https://secret/url"); // mine happens to be available elsewhere on the web
$url = str_replace("wss","https",$url); // nginx doesn't really understand wss, so redirect it as https
header("Location: $url"); // issue a 301 redirect
?>
Within the node red, it's as easy as redirecting to the instance of the nginx, as an added benefit I keep the traffic without SSL downstream of NGINX proxy (ws vs wss), as both instances of node red and nginx are running within the same docker network.