I’m currently developing a web-based messaging system as part of my school project, and I’m seeking some advice on how to proceed effectively.
I’ve been looking into PHP sockets for managing real-time messages. It seems that PHP operates in a linear manner, executing from the top down, and I’ve noticed that maintaining a continuous while loop is essential for listening to new incoming connections, thereby keeping the script active.
That being said, I aim to build a more engaging user experience using JavaScript. My intention is to implement a method that allows JavaScript to check for new messages regularly and update the display without needing a full page reload.
I am seeking guidance on how to structure this application. Should I rely entirely on PHP for the socket handling, or can JavaScript effectively interact with a PHP socket via AJAX? If AJAX is a viable option, would it be possible for the PHP server to return message data formatted as JSON?
As I am quite new to working with PHP sockets, any insights on the feasibility of this JavaScript/AJAX method or possible alternatives for creating a real-time messaging system would be greatly appreciated.
your setup’s solid for a school project! polling every 2 seconds beats wrestling with websockets. just bump up your php max execution time so it doesn’t timeout. one thing - add message ids or timestamps so you’re not pulling the same old messages repeatedly. saves bandwidth and makes it feel faster.
Your polling approach works fine for a school project, but it’s not super efficient. The main problem is you’re hitting the server every 2 seconds whether there are new messages or not. That’s a lot of unnecessary requests and bandwidth.
Try long polling instead. Your PHP script keeps the request open until new data shows up or it times out. Way fewer server hits and still feels real-time.
About PHP sockets with AJAX - don’t go there. It gets messy fast, and PHP wasn’t built for persistent web connections anyway. Your fetch_messages.php approach is actually smarter since it uses normal HTTP requests.
Couple quick improvements: add a timestamp parameter so PHP only sends messages newer than your last one. Less data to push around. Also throw some error handling in your JavaScript for when the server’s down.
I went through this exact same thing building my first messaging app a few years ago. Started with polling like you’re doing, but it doesn’t scale at all - constant HTTP requests hammered the server and created annoying delays. Server-Sent Events (SSE) worked way better. PHP handles SSE well, and you get a persistent connection that pushes updates instantly. Just set up an endpoint that keeps the connection open and streams new messages as they come in. WebSockets are another option, but need more server setup. I switched to Node.js with Socket.io for production since PHP isn’t built for long-running processes like WebSocket servers. If you’re stuck with PHP, Ratchet works but has a steep learning curve. For a school project, SSE’s probably perfect - feels real-time without WebSocket complexity.