r/CodingHelp • u/Virtual_Net9208 • 2d ago
[PHP] Whats the most efficient way to pull messeges in PHP?
I run a small chat site for students at my school (about 140 users). It's built with plain JavaScript on the frontend and PHP on the backend, on shared hosting.
https://hbhub.noahgajnielsen.dk
Right now each open tab polls the server about once per second:
GET /chat/?poll=1&listV=eecc4cd414d8f9c9&requestsV=ef46db3751d8e999
GET /chat/?poll=1&listV=...&requestsV=...&nav=messages%3D1%26after%3D2633listV and requestsV are hashes of the chat list and friend requests. The server compares them and only sends data back when something has changed. after is the ID of the newest message the client already has.
This works, but:
- Every open tab makes ~3,600 requests per hour, even when nothing happens
- Messages can still show up with up to ~1 second of delay
- Many users leave the tab open all days
What I've considered:
- WebSockets: my host doesn't seem to allow long-running processes (e.g. Ratchet or a Node server)
- Server-Sent Events from PHP: I'm unsure whether a PHP script holding a connection open per user will exhaust PHP-FPM workers
- Long polling: same worker concern
Question: On shared hosting without long-running processes, what's the most efficient way to push new chat messages to the browser? Is SSE from PHP practical for ~100 concurrent users, or should I keep polling and back off (for example, slow down when document.hidden is true)?
•
u/AutoModerator 2d ago
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/minhaz1217 2d ago
Websocket is the actual solution.
If you don't want to do it, first option would be to implement caching. If you can't run redis at least do some in memory caching.
ListV and requestV seems a bit over engineered. If your chat has last I'd or sort by time you can just send information that's after that time. No need to implement complex hashing.
Also look into long polling as you are sending one request per second long polling would a little bit later.
Try to implement some kind of halt if the tab is not active. Maybe do some kind of exponential back off type of thing.
Also if you aren't already doing it. You can reduce the DB load or application load by analyzing the access pattern. Like if you have 10 people in a chat. All 10 can receive the same data. So for 10people per second you don't need to call dB 10 times. You can implement some kind of bucket(or cache) that'll return whatever there is in the bucket.
Also messages are more frequent than friend request. So I'm not sure why you are coupling these 2. Message needs to be seen instantly. But if you see a friend request appearing 10-30 sec late, customers might not care? You can add a in place reload(loading data without reloading the page) for checking friend request.
Anyway there are many things you can do to reduce these things. But websocket and caching would be the best way to go.
1
u/Lumethys 1d ago
websocket, switch to new host