'Implement WebSocket on front-end without exact URL

I try to make my web application work with WebSocket and while implementing the front-ent javascript I am requested to give the server url as an argument (e.g var exampleSocket = new WebSocket("wss://www.example.com/socketserver", "protocolOne");). However, I still don't know on which exact domain name my server will be run at and I want my back-end to work versatility without depending on me updating it. Note that the server side script that runs the WebSocket server is the same that runs the Express server to serve the page, the this file is located in the same path with a folder contains all the front-end statics (including the script.js). Any advice on how to implement this?



Solution 1:[1]

The answer is probably to use:

var exampleSocket = new WebSocket(`wss://${window.location.host}/`,"protocolOne");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Iftach