I’m was testing out whether or not I could have a socketio client running on a webpage to recieve messages from a python socketio server. Basically I want to push some messages/data to the client rather than have the client poll for new messages periodically.
I uploaded socket.io.js as an asset and I’m using some custom html for the web page. I have a socketio server written in python running on localhost:5000 on my computer and I’m using the anvil docker image to run my app locally. The app runs without crashing but seems to fail to connect to the socket server with the error messages:
GET http://localhost:5000/socket.io/?EIO=4&transport=polling&t=O944Rk- net::ERR_FAILED 200
Access to fetch at ‘http://localhost:5000/socket.io/?EIO=4&transport=polling&t=O940YKE’ from origin ‘http://localhost:3030’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.
Form1’s custom html:
<script src="_/theme/socket.io.js"></script>
<script>
var socket = io("http://localhost:5000");
var number = this.find("p");
console.log("working?")
socket.on('new_value', function(value) {
console.log(value)
});
</script>
<p>Number</p>
python socketio server:
from aiohttp import web
import socketio
sio = socketio.AsyncServer(ping_timeout=30, cors_allowed_origins =[])
app = web.Application()
sio.attach(app)
@sio.event
async def connect(sid, environ):
print("Connect: ", sid)
if __name__ == '__main__':
web.run_app(app, port=5000)
Clone link: