I created my first node.js app using socket.io. Specifically I implemented the chat example published by socket.io. It works perfectly, locally. And then I tried deploying i
GAE support for persistent socket connections arrived in February 2019!
To make this work, you'll need to be using the flex
environment and modify your app.yaml
to include session_affinity
:
network:
session_affinity: true
Note that I still had to open port 65080 to get this working, but no other changes were required for me.
Read the deets at:
https://cloud.google.com/appengine/docs/flexible/nodejs/using-websockets-and-session-affinity
This app.yaml configuration worked for me:
runtime: nodejs
env: flex
manual_scaling:
instances: 1
network:
session_affinity: true
And I enabled the firewall rules by this command:
gcloud compute firewall-rules create default-allow-websockets --allow
tcp:65080 --target-tags websocket --description "Allow websocket
traffic on port 65080"
Google has an example app using WebSockets here, you need to do the following to get it working correctly:
That should be it (don't take my word for it though, this is what I've been able to find out after doing some research on the docs), hope it helps!
var METADATA_NETWORK_INTERFACE_URL = 'http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip';
function getExternalIp (cb) {
var options = {
url: METADATA_NETWORK_INTERFACE_URL,
headers: {
'Metadata-Flavor': 'Google'
}
};
request(options, function (err, resp, body) {
if (err || resp.statusCode !== 200) {
console.log('Error while talking to metadata server, assuming localhost');
return cb('localhost');
}
return cb(body);
});
}
gcloud compute firewall-rules create default-allow-websockets \
--allow tcp:65080 \
--target-tags websocket \
--description "Allow websocket traffic on port 65080"
In short this cannot be done on production and it appears to be work in process. The right architecture is to have a chat server on google compute engine as outlined here.
But as a proof of concept to use socket.io on google app engine is very similar to that shown in google appengine samples for websockets.
In case of socket.io do the following steps on server side. Code snippet below.
socket.io changes on server side
var app_chat = require('express')();
var server1 = require('http').Server(app_chat);
var io = require('socket.io')(server1);
server1.listen(65080);
io.on('connection', function (socket) {
console.log('user connected');
socket.on('chat_message', function (data) {
console.log('client sent:',data);
socket.emit('chat_message', 'Server is echoing your message: ' + data);
});
});
open firewall by command
gcloud compute firewall-rules create default-allow-websockets \
--allow tcp:65080 \
--target-tags websocket \
--description "Allow websocket traffic on port 65080"
I hope Google comes up with a production-ready solution soon enough on as this will become a key armour in any PaaS-arsenal.