0

Hie,

I have a flask server with a React client and I'm using flask_socketio to create a socket and socket.io-client on my React client to connect to it

I'm able to send and receive messages just fine by using the following code on my server:

message = {"hello": "world"} 

with app.test_request_context('/'): 
    send (message, broadcast=True, namespace="/") 

And this code on in my client:

import io from "socket.io-client" 

let endPoint = "http://localhost:5000"; 
let socket = io.connect (`${endPoint}`); 

To connect to the socket and this code to act on messages:

useEffect(() => {  

    socket.on ("message", msg => {
        console.log (msg); 
    }) 
}, []); 

However! When I moved the code sending the messages to another file and then called it like this: (app is a global variable representing my flask app)

from app import app 

with app.test_request_context('/'): 
    send ({"hello": "world"}, broadcast=True, namespace="/") 

The message is no longer received on the client side as before

Any thoughts?

Lior Z
  • 668
  • 1
  • 9
  • 21

1 Answers1

0

Solution was to import the "app" from main and not from app:

from __main__ import app

as explained here: Flask socket.io message events in different files

Lior Z
  • 668
  • 1
  • 9
  • 21