1

I have a working node.js express based server (and client) application here that shows RPC over http+websockets. This works perfectly when run locally (using devcontainers) and includes the Dockerfile as well as devcontainer.json. However, when run from a codespace, it fails with the following client-side error messages.

client.js:9 Mixed Content: 
The page at 'https://aniongithub-jsonrpc-bidirectional-example-<redacted>-8080.preview.app.github.dev/' 
was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint
'ws://aniongithub-jsonrpc-bidirectional-example-<redacted>-8080.preview.app.github.dev/api'. 
This request has been blocked; this endpoint must be available over WSS.

(anonymous) @ client.js:9
client.js:9 Uncaught DOMException: Failed to construct 'WebSocket': 
An insecure WebSocket connection may not be initiated from a page loaded over HTTPS 
at 'https://aniongithub-jsonrpc-bidirectional-example-<redacted>-8080.preview.app.github.dev/client.js:9:10'

The documentation here states that By default, GitHub Codespaces forwards ports using HTTP but you can update any port to use HTTPS, as needed. When I check the settings indicated:

CodeSpaces forwarded ports settings

it's set to http. What am I missing here? How can I get it to serve my express application over http?

Note: My intention is that when locally cloned and opened in a devcontainer, the code works just as it would if opened in a CodeSpace. This means I need to ensure that the certs generated by CodeSpaces are somehow factored into my local devcontainer process or that I forego authentication altogether. Alternatively, I need to find out if I'm running on CodeSpaces and do different things, which seems messy and shouldn't be the case. Hope this makes my intentions for asking this question clearer!

4
  • Your local address is https but it is trying to connect to the WS over http, You need to change the "ws://aniongithub-jsonrpc-bidirectional-example-<redacted>-8080.preview.app.github.dev/api" to wss://your_socket_url Commented Oct 31, 2022 at 11:49
  • @Aditya_Anand that might work for the CodeSpaces version, but then my local devcontainer version wouldn't. I'm trying to find a solution where both the local devcontainer version and CodeSpaces version "just work", which seems to be to use http (and which the docs indicate should be possible). Edited my question the add this clarification. Commented Oct 31, 2022 at 17:26
  • I believe that can be handled with the .env file that you are using in different environments, The protocol (http or https) can be defined there in the .env file. which can then be used by your docker compose based on the deployment environment. Commented Nov 3, 2022 at 7:56
  • @Aditya_Anand I'm not sure you understand, devcontainers are created by VSCode based on the devcontainer.json file and the Dockerfile (or docker-compose.yml) specified there. Unfortunately, since I don't deploy it - I ask Github to open a repo in a Codespace for me, this isn't something I can pass an argument to. In fact, that's my question - how does one detect if they're running in a Github CodeSpace? Commented Nov 16, 2022 at 3:22

1 Answer 1

0

It turns out that I just couldn't use http for the RPC endpoint when running over https, so the solution was to use location.protocol and ws/wss depending on the current protocol to initialize the client RPC endpoint.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.