Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi forum,

to use Socket.IO, our server has to use a library called flask_socketio. Its clients will be browsers. They need a Socket.IO implementation, too. Therefore, the index.html that the server distributes includes
JavaScript
<script src="/socket.io/socket.io.js"></script>
The idea is that client will ask server for this JavaScript file and server serves it. Therefore server can be assured that client has a version that is compatible with server.

But in debug, I see a
127.0.0.1 - - [24/Feb/2021 16:19:34] "GET /socket.io/socket.io.js HTTP/1.1" 400 -
every time I update the browser.

What I have tried:

So I implemented a route
Python
@app.route('/socket.io/<sth>')
def ServeSocketIoClientImplementation():
    return render_template("socket.io.js")
It doesn't seem to work.

How is flask-socketio supposed to deliver the client SocketIO implementation?

*** UPDATE ***
I have two working versions (well, kind of):
1)
HTTP
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
This one works, but needs an internet connection which is not desirable in the target environment.
2)
HTTP
<script src="{{ url_for('static', filename = 'socket.io.js') }}"></script>
The socket.io.js file is in a subdirectory "/static/socket.io.js" of my python-flask-socket.io program. It doesn't require internet access, but doesn't use WebSocket apparently and falls back to long polling instead.

How can I combine local availability with the use of WebSockets under Socket.IO?
Posted
Updated 10-Mar-21 21:38pm
v2

1 solution

 
Share this answer
 
Comments
lukeer 5-Mar-21 10:58am    
I did, but in this version, the socket.io.js is taken from a cdn (3rd code box).
The crossorigin part seems to be an issue. I get an error message in my client firefox's console that says that this is a security risk and that it won't open the file.

I have, however, tried this cdn approach and do indeed get a connection just by leaving out the crossorigin and integrity parts.

But, I'm not very fond of having to get code from an external source every time User loads the website. This[^] example uses a local copy (see 8th code box)
<script src="/socket.io/socket.io.js"></script>
But I don't understand what I have to do to make this work for me as well.
Richard MacCutchan 5-Mar-21 11:14am    
Sorry, I have not used this implementation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900