Home > OS >  Implement WebSocket on front-end without exact URL
Implement WebSocket on front-end without exact URL

Time:03-27

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?

CodePudding user response:

The answer is probably to use:

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