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!

Ani
  • 10,826
  • 3
  • 27
  • 46
  • 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--8080.preview.app.github.dev/api" to wss://your_socket_url – Aditya_Anand Oct 31 '22 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. – Ani Oct 31 '22 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. – Aditya_Anand Nov 03 '22 at 07: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? – Ani Nov 16 '22 at 03:22

1 Answers1

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.

Ani
  • 10,826
  • 3
  • 27
  • 46