0

The google appengine documentation states websockets are supported, but in their example they use socket.io instead of a "regular" WebSocketServer.

my (simplified) nodejs server is super basic:

import WebSocket, { WebSocketServer } from 'ws';
const wss = new WebSocketServer({port: 1234});
wss.on('connection', (connection: WebSocket) => {
 connection.binaryType = 'arraybuffer';
 console.log('connected');
 
 connection.on('message', async (message: ArrayBuffer) => {
 console.log('message received');
 });
})

this is my app.yaml

runtime: nodejs
service: websocket
env: flex
runtime_config:
 operating_system: "ubuntu22"
 runtime_version: "22"
manual_scaling:
 instances: 1

locally it runs just fine, but hosted in gcloud my client is not able to connect using

// locally:
new WebSocket('ws:localhost:1234/id-123', []);
// or in staging:
new WebSocket('wss:websocket-dot-my-staging.ey.r.appspot.com:1234/id-123', []);

telling me:

WebSocket connection to 'wss://websocket-dot-my-staging.ey.r.appspot.com:1234/id-123' failed

Is it related to the secure connection that is built by the engine? Is it even possible with the appengine? I am not planning to switch any of the used technologies, since I spent quite some time setting up the app as it is, so I would love to find the root cause instead of switching to socket.io or Cloud Run etc. Background: I am trying to set up a yjs websocket server.

gre_gor
6,65312 gold badges104 silver badges105 bronze badges
asked Jun 24 at 22:46
1
  • Wow, a downvote but not even a comment :( I am running in circles :/ Commented Jun 25 at 6:12

1 Answer 1

1

Part of the contract of App Engine is that your code must configure itself to use port 8080 or the value of the PORT environment variable provided by App Engine.

Commonly (in JavaScript) you can:

const PORT = process.env.PORT || 8080

And then:

const wss = new WebSocketServer({port: PORT})

I tried your code and making this change worked for me.

App Engine proxies (the container of) your code through TLS and so you will want to access the WebSocket server via the App Engine provided URL and port 443. I used websocat to test and:

websocat wss://{app-engine-host}

Or:

HOST=$(\
 gcloud app describe \
 --project=${PROJECT} \
 --format="value(defaultHostname)")
websocat wss://${HOST}
halfer
20.2k20 gold badges111 silver badges208 bronze badges
answered Jun 26 at 14:35
Sign up to request clarification or add additional context in comments.

3 Comments

Dude, thank you so much! Thanks for pining my on the other question, I wasted days with this problem! Could I have known better? Is this info documented anywhere? I will go buy you a coffee now!
You're very welcome! I'm very pleased to have been able to help. I worked in Google Cloud for many years and so this is just stuff I know. I think a problem Google has with its documentation is that it provides a single view for every developer and so important details aren't surfaced. I mentioned a "contract" in my other comment and am unable to find App Engine's but Cloud Run (very similar) has one (contract) and it explains this port requirement
Wow! Thank you very much for the coffees! I appreciate that very much.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.