Question

Node.js/Socket.io client side environment variable APP patform

Hi all, I am having a problem with creating a ‘Deploy to DO’ button for my app on the APP Platform. I am having a socket.io/node.js application and I need to pass an environment variable on my client-side:

let socket = io.connect('https://some-url.com', {
    reconnection: false,
    transports: ['websocket'],
    upgrade: false
});

I need to add an environment variable to pass the URL upon the deployment of the app. I tried with ${APP_URL} and with process.env.APP_URL but neither of those seems to work.

Thanks in advance!


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hey all, I somehow managed to use a workaround and I’m sharing it here in case somebody faces the same problem. I used the following code:

let url = window.location.href;
const {hostname} = new URL(url);

let socket = io.connect(`https://${hostname}`, {
    reconnection: false,
    transports: ['websocket'],
    upgrade: false
});

I am defining a variable to get the current URL, then I am using the URL constructor to extract the URL. I am then making a string with https://${hostname} to put the URL in use.