Report this

What is the reason for this report?

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

Posted on January 4, 2021

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!



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!

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.

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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.