# Changing Websocket url, how to deal with it?

**URL:** <https://discourse.nodered.org/t/changing-websocket-url-how-to-deal-with-it/10603>\
**Category:** General\
**Created:** [27 April 2019 10:13 UTC](https://discourse.nodered.org/t/changing-websocket-url-how-to-deal-with-it/10603 "2019-04-27T10:13:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [27 April 2019 10:13 UTC](https://discourse.nodered.org/t/changing-websocket-url-how-to-deal-with-it/10603/1 "2019-04-27T10:13:17Z")

</div>

Hello,

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](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!).

Can anyone suggest a viable workaround?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [27 April 2019 12:15 UTC](https://discourse.nodered.org/t/changing-websocket-url-how-to-deal-with-it/10603/2 "2019-04-27T12:15:33Z")

</div>

I would begin looking at the source. Websockets exist for interaction and should be available at a single endpoint.

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [27 April 2019 17:10 UTC](https://discourse.nodered.org/t/changing-websocket-url-how-to-deal-with-it/10603/3 "2019-04-27T17:10:28Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [1 May 2019 06:45 UTC](https://discourse.nodered.org/t/changing-websocket-url-how-to-deal-with-it/10603/4 "2019-05-01T06:45:14Z")

</div>

I've located the code for the websocket node here:

> <https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/nodes/core/io/22-websocket.js>

I just need to work out how to add an option whereby the url can be set via a msg...

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [1 May 2019 10:52 UTC](https://discourse.nodered.org/t/changing-websocket-url-how-to-deal-with-it/10603/5 "2019-05-01T10:52:50Z")

</div>

> Sadly, the endpoint does change (like one of IBM's own Watson endpoints!).

these are static according to their [documentation](https://cloud.ibm.com/docs/services/speech-to-text?topic=speech-to-text-websockets).

---

<div class="post-metadata">

**Author:** ![ecrode](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ecrode/32/25252_2.png) [@ecrode](https://discourse.nodered.org/u/ecrode)\
**Post date:** [12 May 2020 15:00 UTC](https://discourse.nodered.org/t/changing-websocket-url-how-to-deal-with-it/10603/6 "2020-05-12T15:00:18Z")

</div>

I ended up solving the URL issue with NGINX, here is my config:

```auto
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:

```auto
<?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.

![red](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6ad1140c3057dc29492ac23b1ba75cb61fa3890.jpeg)

Cheers,  
Gene

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 May 2020 15:00 UTC](https://discourse.nodered.org/t/changing-websocket-url-how-to-deal-with-it/10603/7 "2020-05-26T15:00:21Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
