Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deploy socket.io to Google App Engine?

Tags:

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 it to Google App Engine (making some code tweaks for node to work).

Everything shows up indicating that the node part is working well. However the chat doesn't work indicating that socket.io part isn't working. You can see the deployed app (and page source) here.

Do I have to do anything additional? Something in the yaml or json files?

yaml content:

runtime: nodejs vm: true  skip_files:   - ^(.*/)?.*/node_modules/.*$ 

json content:

{   "name": "Chaty",   "description": "chatrooms app",   "version": "0.0.1",   "private": true,   "license": "Apache Version 2.0",   "author": "McChatface",   "engines": {     "node": "~4.2"   },   "scripts": {     "start": "node app.js",     "monitor": "nodemon app.js",     "deploy": "gcloud preview app deploy"   },   "dependencies": {     "express": "^4.13.4",     "socket.io": "^1.4.6"   } } 
like image 635
Rian Smith Avatar asked May 23 '16 21:05

Rian Smith


People also ask

Does Google App Engine support WebSockets?

Try Google Cloud Today, we are excited to announce that App Engine Flexible Environment now supports the WebSocket protocol in beta—the first time that App Engine supports a streaming protocol.

How do you use Socket.IO in electron JS?

So you should create Socket server at somewhere sth like Cloud server and your electron app should contain one socket.io client instance. // with ES6 import import io from 'socket. io-client'; const socket = io('http://localhost'); So that users can communicate inside your Electron app.


1 Answers

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.

  1. Create second express middleware and server.
  2. Attach/use socket.io with new server.
  3. Listen to port (65080).
  4. Open firewall for port (65080) on google compute engine.
  5. Link to working repository.

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.

like image 192
npr Avatar answered Sep 28 '22 07:09

npr