Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom handshake data with Flask-SocketIO

I have a Python server using Flask, that has a websocket connection using Flask-SocketIO . There is a similar question : Send custom data along with handshakeData in socket.io? The idea is to do the same thing but instead of using Node, using Flask. I want the client to send some data in the connect event, for example:

var socket = io("http://127.0.0.1:3000/", { query: "foo=bar" });

I haven't been able to get that custom data, and I can't rely on cookies due to the client's framework. A working solution is to have the connect event as usual, and then, in a custom event, get that information as a payload. But what we would want is to only have to do a connect event. Thanks!

like image 411
Naye Nati Cases Casas Avatar asked Apr 26 '16 12:04

Naye Nati Cases Casas


1 Answers

As Miguel suggested in his comment you can simply do

from flask import Flask
from flask import request

app = Flask(__name__)
socketio = SocketIO(app)

@socketio.on('connect')
def connect():
    foo = request.args.get('foo')
    # foo will be 'bar' when a client connects

socketio.run(app)

on the server side.

like image 93
Rouven B. Avatar answered Sep 30 '22 13:09

Rouven B.