Webscraping Tweets -> display in Django app

Hi everyone,
wondering how can I display tweets that i scraped from twitter.
I am web-scraping tweets in special script and saving it to psql which django is connected on.
I am using channels and weboscket so tweets can be displayed. Now I want to make it so if user connects on my site it display him latest tweets(I am keeping only last 10 tweets) and keeps displaying the newest tweets.
(I want my script running but only if there is 1 client on that network).
Any help?

What specifically is your question or issue for which you are requesting assistance?

How can I run script exactly when user connects and script stops when user disconnects?

If I had to do this, I’d create a separate Channels worker process to run that scraping script. The script itself would run continuously until there are 0 users connected. That script would likely also listen for messages like “user_connected” and “user_disconnected” that tracks each user. The first “user_connected” message received would start the script.

Then, in the Websocket consumers, I’d add code to the connect and disconnect methods to send the appropriate message to the worker process.