3

I'd like to run a p2p chat written in node js with socket io on GAE.

My app works locally fine but I get error messages when I run it on the GAE servers related to the socket io I think.

Here are the two relevant script tags of my local client.html when running locally:

<script src="/socket.io/socket.io.js"></script>
<!-- <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script> -->
<script type="text/javascript">
    // our socket.io code goes here
    var socketio = io.connect("127.0.0.1:1337");
    socketio.on("message_to_client", function (data) {
        to_history(data['message']);
    });

    function send_message() {
        var msg = [document.getElementById("text1").value, user1, uuid];

        socketio.emit("message_to_server", {
            message: msg
        });
    }

</script>

I've seen blogs/posts saying that for deployment I need to allow a firewall rule here on SO (which is in place now). I also tried pointing my deployed app to a static external IP like (after making it static in my google cloud console):

var socketio = io.connect('https://104.197.51.XXX')

or to point it to the port 65080 specified in my firewall rule (see documentation by google here:

var socketio = io.connect('https://104.197.51.XXX:65080')

None of this works.

I have the html loaded fine and the jQuery part I have and css is also loading just fine. It's just the socket stuff that I seem to be getting wrong. What do I have to change?

If this is of use, here the app.yaml:

runtime: nodejs
vm: true

Any help is greatly appreciated. Thanks.

Community
  • 1
  • 1
ben_aaron
  • 1,504
  • 2
  • 19
  • 39

2 Answers2

5

Sadly, App Engine just doesn't support websockets (yet). The hack-around you're using is really unreliable for a few reasons:

  • it makes a direct connection to the instance, which can go down or be recycled at any time
  • Short of magic hackery, there's really no way to get https going down this route.

In short - this is not production ready. That having been said....

https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/websockets

That link will show you a working example and instructions of how to set it up.

Instead of that - I'd suggest using pubnub: https://www.pubnub.com/docs/nodejs-javascript/pubnub-javascript-sdk

It has a really nice API, and is going to be way more reliable than anything you can hack together with App Engine (you know, until we fix this). You can see a few examples that I've done here:

I hope this helps!

Justin Beckwith
  • 7,686
  • 1
  • 33
  • 55
  • Thanks. This is really helpful. I'd need my app atm for very small-scale data collection only, so maybe a workaround as outlined in the first link would do. I also found this [other example of websockets in google cloud](https://cloud.google.com/solutions/real-time-gaming-with-node-js-websocket). Why I'm hesitant to switch to pubnub is that my app is build with all controls I want now and I don't know whether a rebuild is worth it. – ben_aaron Aug 14 '16 at 14:58
  • Wait what? You show an example of App Engine working with websockets yet you say it still doesn't work? May you please elaborate! – basickarl May 30 '17 at 07:44
  • As I explained it - it will kinda work. It's just really unreliable, doesn't support https, and will just randomly stop working for a while when your VM goes down. – Justin Beckwith May 30 '17 at 22:30
  • 1
    Do you have any plans to add websockets support in near future? – Cevat Barış Yılmaz Oct 10 '17 at 22:18
1

I also made the similar chat webapp recently and deployed it on heroku (https://chatterboxxx.herokuapp.com). I also used socket.io for this. I am not sure of GAE, but I don't think you need to specify any IP address in your socket.io js code. I think you should use

var socketio = io();

instead of

var socketio = io.connect("127.0.0.1:1337");

This works well for me.

Aman Dwivedi
  • 174
  • 1
  • 10