r/CodingHelp • u/Virtual_Net9208 • 12h 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)?