'Unable to establish connection in Flask-socketio with namepaces[routes]
I am trying to emit a event on route/namespace "/chat" but I keep on getting error /chat is not a connected namespace.
Here is my server side code:
from config import socket
from flask_socketio import send, emit
from flask import Blueprint
admin_sockets = Blueprint('admin_sockets', __name__)
@socket.on('connect')
def test_connect(auth):
emit('my response', {'data': 'Connected'})
@socket.on('getcase', namespace='/chat')
def handleMessage(msg):
print('YOOOOHOOOO: ' + str(msg))
send(msg, broadcast=True)
# def tryy():
# '''rwre'''
@socket.on('message')
def handleMessage(msg):
print('Message: ' + str(msg))
send(msg, broadcast=True)
And here is the client-side code:
import socketio
# standard Python
sio = socketio.Client()
@sio.event
def connect():
print("I'm connected!")
sio.emit(
'message', {'userKey': 'Your Streaming API Key'})
@sio.event
def connect_error():
print("The connection failed!")
@sio.on('handshake')
def on_message(data):
print('HandShake', data)
# sio.emit('message', {'symbol': 'EURUSD'})
@sio.on('message')
def on_message(data):
print('Price Data ', data)
sio.emit('getcase', {'userKey': '2'}, namespace='/chat')
But when I run the code I get exception as
I'm connected!
Price Data {'userKey': 'Your Streaming API Key'}
Exception in thread Thread-5:
Traceback (most recent call last):
File "C:\Users\jayendra\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\Users\jayendra\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\jayendra\AppData\Local\Programs\Python\Python39\lib\site-packages\socketio\client.py", line 705, in _handle_eio_message
self._handle_event(pkt.namespace, pkt.id, pkt.data)
File "C:\Users\jayendra\AppData\Local\Programs\Python\Python39\lib\site-packages\socketio\client.py", line 581, in _handle_event
r = self._trigger_event(data[0], namespace, *data[1:])
File "C:\Users\jayendra\AppData\Local\Programs\Python\Python39\lib\site-packages\socketio\client.py", line 629, in _trigger_event
return self.handlers[namespace][event](*args)
File "C:\tai_jutsu\hackathon_v3\sih-backend\websocket_client.py", line 35, in on_message
sio.emit('getcase', {'userKey': '2'}, namespace='/chat')
File "C:\Users\jayendra\AppData\Local\Programs\Python\Python39\lib\site-packages\socketio\client.py", line 393, in emit
raise exceptions.BadNamespaceError(
socketio.exceptions.BadNamespaceError: /chat is not a connected namespace.
Can anyone please tell me where is the bug, thanks in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
