python-socketio: sid's don't match

Hi Miguel, I have a flask-socketio server which must communicate with web browsers (JavaScript) as well as an API for mobile users. In order to test the API socketio functions, I’ve written a small python-socketio client. But the sid shown in this client is not the same as the sid reported on the server. To make it clearer:

Server side blueprint for API:

"""SocketIO Events."""

import logging

from flask import request, session

from app import socketio, redis_store


logger = logging.getLogger(__name__)


@socketio.on('connect', namespace='/api')
def connect():
    """Log connect."""
    logger.info(f"Connect from api: {request.sid}")
    session['room'] = request.sid


@socketio.on('disconnect', namespace='/api')
def disconnect():
    """Log disconnect."""
    logger.info(f"Disconn from api: {session.get('room')}")


@socketio.on('join', namespace='/api')
def join(data):
    """Join with id."""
    id = data['id']
    room = session.setdefault('room', request.sid)
    logger.info(f"user_{id} has joined: {room}")

Client simulation:

"""Simulate a mobile app user."""

import requests
import json

import socketio

import logging

from config import ServerConfig


logging.basicConfig(level=logging.INFO,
                    format="[%(asctime)s] %(levelname)s %(name)-32s "
                           "%(message)s")

logging.getLogger('socketio').setLevel(logging.WARNING)
logging.getLogger('engineio').setLevel(logging.WARNING)

logger = logging.getLogger('sio')


IP = ['192.168.1.12', '10.164.0.2'][0]
SERVER = f'http://{IP}:{ServerConfig.PORT}'

sio = socketio.Client()


@sio.on("connect", namespace="/api")
def connect():
    """Log connect."""
    logger.info(f"Connected: {sio.sid}")


@sio.on("disconnect", namespace="/api")
def disconnect():
    """Log disconnect."""
    logger.info("Disconnected.")


def main():
    """Simulate a mobile app user."""
    response = requests.post(SERVER + '/api/v2/auth', data={},
                             auth=('xxxx@gmail.com', 'yyyy'))
    user = json.loads(response.content)
    print(user)

    sio.connect(f'http://{IP}:{ServerConfig.PORT}',
                namespaces=["/api"])
    logger.info(f"sid: {sio.sid}")
    sio.sleep(1)
    sio.emit('join', {'id': user['customerID']},
             namespace="/api")

    sio.send('hola, que tal?',
             namespace="/api")

    sio.sleep(5)
    sio.disconnect()


if __name__ == '__main__':
    main()

The sid reported by the server at the line logger.info(f"Connect from api: {request.sid}") is not the same as the sid reported by the client at logger.info(f"Connected: {sio.sid}") (which is the same sid at logger.info(f"sid: {sio.sid}")

For the equivalent in JavaScript, I don’t have any problem. (The sids match). Can you see why this could be happening? Thanks

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

To get the sid of your client you have to use the sio.get_sid(namespace='/api') method.