miguelgrinberg / simple-websocket

Simple WebSocket server and client for Python.
MIT License
79 stars 17 forks source link

Can I use a different api for the client and still connect to the simple-websocket server ? #12

Closed Cigomba closed 2 years ago

Cigomba commented 2 years ago

This is probably a dumb question, but I've researched for about 3 days now and I haven't figured it out yet. I'm trying to make a chat web app in Flask using the simple-websocket api. My server's running on localhost using the server api from simple-websocket, and I want to use the Websocket api for the client so that I can connect using any web browser that supports the Websocket api.

I just don't know how to connect the simple-websocket client in the browser. Is it necessary to use the simple-websocket client api, or can I use the websocket api for the client:

const socket = new WebSocket('ws://localhost:5000/echo');

The main errors I'm getting are cors related as well:

127.0.0.1 - - [21/May/2022 20:29:26] "GET /socket.io/?EIO=4&transport=polling&t=O3fq0Z6 HTTP/1.1" 404 -

By the way, I think the the simple-websocket api is an awesome little api for people like me who didn't even know flask existed until a few weeks ago. I know I can use better apis like flask-sock or socketio and I plan on making chat apps with them in the future, so I can really get a good grasp on websockets. Thanks in advance for any help!

miguelgrinberg commented 2 years ago

Yes, you are going at this the wrong way. It is actually much easier, you seem to be overcomplicating things.

First of all, you are mixing two things: WebSocket and Socket.IO. Which one do you want? These are different, so you have to decide which one is the one that you want to use. The source of your confusion might be that Socket.IO uses WebSocket, so in that sense WebSocket is more primitive than Socket.IO.

If you want to use Flask with WebSocket, then you can add the Flask-Sock extension to your Flask application. Flask-Sock uses simple-websocket internally, and integrates it nicely with Flask.

If you want to use Flask with Socket.IO, then you can add the Flask-SocketIO extension to your Flask application. This extension can work with a number of WebSocket packages, and simple-websocket package is one of the options.

Okay, so this is all for the server. Now for the client, you can use any client, but you have to match the server that you are using. If you are using a WebSocket server, then you have to use a WebSocket client, and if you are using a Socket.IO you have to use a Socket.IO client. Your chosen client needs to run in the browser, so you'll be looking for JavaScript clients. Even though there are Python WebSocket and Socket.IO clients available, if you want to build a web application that runs in the browser, then you have to use the JavaScript clients, not the Python clients.

Hope this helps clarify things a bit.

Cigomba commented 2 years ago

Hi Miguel,

Thanks for replying so quickly and thanks for clarifying. I apologize for the late reply as I waited to fix it before replying. I've gotten it to work connect using the native WebSocket client in web browsers as you said. The reason it wasn't connecting initially was that the document wasn't ready before executing the connection request.

Thanks again for your help. I greatly appreciate it.