BugHunterUK
BugHunterUK

Reputation: 8968

How are realtime applications implemented? Websockets/PHP

I want to create a web application where the UI updates in real time (or as close to real time as you're going to get). Data for the UI comes from the server.

I don't want to use traditional HTTP requests where I constantly send requests to the server for new data. I'd rather have a connection open, and have the server push data to the client.

I believe this the publisher/subscriber pattern.

I've heard people mention zeromq, and React and to use Websockets. But from all the examples I've looked at I can't really find anything on this. For example zeromq has examples that show server and client. Do I implement the server, and then use websockets on the UI end as the client?

How would something like this be implemented?

Upvotes: 0

Views: 115

Answers (1)

André Catita
André Catita

Reputation: 1323

Traditional HTTP Requests is still what all of this is all about still.

You can have Regular HTTP Requests: - Users sends Request to Server - Server responds to said request

There's also Ajax Polling and Ajax Long Polling, the concept is similar.

Ajax Polling means, an HTTP request is sent every X seconds to to look for new information. Example: Fetch new Comments for a section.

Ajax Long Polling is similar, but when you send a request to the server, if there are no responses ready to give to the client, you let the connection hang (for a defined period of time). If during that time new information comes in, you are already waiting for it. Otherwise after the time expires, the process restarts. Instead of going back and forth, you send a request - wait, wait - and whether you receive a response or not, after a period of time, you restart the process.

WebSockets is still an HTTP Request. It consists in the client handling the weight in front-end, by opening WebSocket request to a destination. This connection will not close - and it will receive and send real-time information back and forth. Specific actions and replies from the server, need to be programmed and have callbacks in the client side for things to happen. With WebSockets you can receive and transmit in realtime, it's a duplex bi-directional connection.

So yes, in case it wasn't clear. You setup a WebSocket Server, running on a loop, waiting for connections. When it receives one, there's a chat-like communication between that said server and the client; the client who needs to have programmed callbacks for the server responses.

Upvotes: 1

Related Questions