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.
-
Wow, a downvote but not even a comment :( I am running in circles :/Basti– Basti2025年06月25日 06:12:23 +00:00Commented Jun 25 at 6:12
1 Answer 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}
3 Comments
Explore related questions
See similar questions with these tags.