-2

Currently, I have a dynamic website that runs on Flask and I'd like to implement a chat client in the back-end that utilizes websockets that will connect to an external server. So, for example, I'd like to start a websocket to this external chat server when the logged-in user visits the "/chat" route.

Then, I'll handle sending/receiving messages with JavaScript on the page that POSTs a JSON object to another route, such as "/send_msg" while constantly checking for new messages by GETing from "/receive_msg".

The problem is that every single tutorial I've seen on the web (including questions on SO) have been about local websockets where the flask app itself acts as a chat server. And using python's built-in socket module is no good (I think) because I don't see a way to keep it open while the user is on the page and then close()-ing it when the user leaves the page. Is there any way to do this for an external server at all? If so, how do I go about doing this? Thanks! <3

simboyd
  • 93
  • 1
  • 8
  • of coarse... just run the websocket on the server and connect to an ip that is not localhost, but the ip of the server... but your case really doesnt make anysense ... why not just have the js talk directly to the websocket server ... by doing posts, and gets you are basically making it not a websocket server ... and losing all the benefits that a websocket server offers – Joran Beasley May 21 '20 at 18:41
  • @JoranBeasley Hello, Joran! Thanks for your comment. This is just an experiment/exercise. Care to elaborate how the websockets would work in Flask? Theoretically, if there were 10 000 users chatting at the same time, would it be able to sustain all 10 000 connections? Additionally, I still can't see how I accomplish this... I need to be able to listen to the TCP connection for any new messages, which would require me to run a listening function in an infinite loop, which in turn would prevent the chat page from loading... – simboyd May 21 '20 at 19:03
  • nope you just do `let my_chat_socket = new WebSocket("https://my_url.for.web/socket"); my_chat_socket.on_message = my_message_handler_function` websockets should be able to handle hundreds of thousands of concurrent connections ... but it all depends on your hardware, and what the server is doing in addition (ie if its doing hard calculations maybe it would not work so well with that many) – Joran Beasley May 21 '20 at 19:04
  • Thanks Joran, but I was looking for a way to accomplish this in the back-end. I understand it's not the best way to go but as I said this is just exploration and experimentation on my part. – simboyd May 21 '20 at 19:10
  • thats not a viable option as the backend cannot maintain a persistent connection, it will close as soon as it finishes .... this is like saying I got a new gun, how can i do an experiment where I point it at my own head and shoot a bullet, but then i can analyze the bullet, i dont want someone else to analyze it, I need to... but it has to have gone through my head first – Joran Beasley May 21 '20 at 19:13

1 Answers1

0

you are mixing your technology terms up I think

Flask is a webframework (there are some libraries that can offer websockets here), generally speaking this has nothing to do with websockets

It serves HTML/JS/CSS... you would use js to connect to your websocket server as follows

let ws = new WebSocket("ws://my.funky.ws/url")
ws.on_message = (data)=>{
  console.log("Got a websocket message1!!!:",data)
}
ws.on_close = ()=>console.log("Closed websocket connection!")
ws.on_open = () => console.log("Opened websocket server connection successfully!")

you run a websocket server, the javascript (or non-javascript) clients connect to (python-websocket-server is a pretty easy one to use iirc), but this is typically a seperate application from your flask server, (it can still import sqlalchemy models or functions or whatever))

if you really want just ajax calls to posts/gets to check for messages you would use a database table of some sort, that you then put new messages into and query for new messages periodically... but that is not a websocket server... socket servers have no concept of historical state... you only see messages that are sent while you are listening for them ...

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Got it - back-end is a bad way to go about implementing this. I'll just use javascript as per your suggestion. Thanks! – simboyd May 21 '20 at 19:48