I have a basic websocket server app written inFlask-SocketIO, sitting behind NGINX in AWS. I can successfully connect to it using a web client (both Chrome and FFox) but it keeps using polling rather than web sockets. When testing I see messages like the below in the console over and over:
127.0.0.1 - - [2018-04-27 11:59:43] "GET /socket.io/?token=1234567890qwertyuiop&EIO=3&transport=polling&t=1524830363623-23&sid=c406e264e3ac4a06b22a1b0d4f08cf5d HTTP/1.1" 200 191 25.966415
After some research, I added "rememberTransport: false" to the client connection options but that hasn't helped, so presumably it is something wrong with my code or config. I am hoping someone might be able to spot an obvious (noob) error I have made.
Update 29/4/2018
I altered my AWS security group so I could bypass NGINX and access the test WSGI server direct. So now I am using http://serverIP:5000 from my local PC. I am still getting the same issue, so it cant be anything to do with the NGINX config.
I just copied the sample code to my RPi3B and ran the server there. My Samsung phone browser, PC FFox, PC Chrome and RPi Chromium all result in a polling connection. So it looks like it is the Flask-SocketIO server that is the problem. My code is as simple as it can be so what can be wrong?
My server code is as follows:
#!/usr/bin/env python
from flask_socketio import SocketIO, join_room, send, emit, disconnect
from flask import Flask, render_template, request
robotAIapp = Flask(__name__)
socketio = SocketIO(robotAIapp)
@robotAIapp.route('/wsLogin.html')
def wsLogin():
return render_template('wsLogin.html')
@socketio.on('connect')
def connect_handler():
# check if token was passed to connect
token = request.args.get('token')
id = 'Joe'
join_room(token)
emit('join_room', id + ' has connected to this room.', room=token)
if __name__ == "__main__":
socketio.run(robotAIapp, host= '0.0.0.0', debug=True)
My client code looks like the following:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flask SocketIO Test</title>
</head>
<body>
<p>Some sample code to make sure Flask-SocketIO works.</p>
<button onclick="connectWS()">Connect</button>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
<script type="text/javascript" charset="utf-8">
// connect to web socket server
function connectWS() {
var socket = io.connect('http://ec2-13-54-68-85.ap-southeast-2.compute.amazonaws.com',
{rememberTransport: false, query: "token=1234567890POIUYTREWQ" }
);
// verify our websocket connection is established
socket.on('connect', function() {
console.log('Websocket connected!');
});
// message handler for 'join_room' messages
socket.on('join_room', function(msg) {
console.log('join_room ' + msg);
});
}
</script>
</body>
</html>
And finally, the relevant bits from my NGINX config are
#Redirect to API
#--------------------------------------------------------------
location /api/ {
proxy_pass http://127.0.0.1:5000/;
}
#Redirect web socket connections
#--------------------------------------------------------------
location /socket.io {
#include proxy_params;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://127.0.0.1:5000/socket.io;
}
pip freezeto your question? - Miguel