Redan Hassoun
Redan Hassoun

Reputation: 106

Make client listen to server

How can i implement a mechanism in which i can send data from server to client?

I have implemented a java app in which the client listens for a URL in the server on a specific frequency ( the server side is implemented using PHP). Every X time the client opens connection and checks the returned json from the server for updates.

I think this is not a good convention, because in this method the client keeps sending HTTP requests to the server so the server has too many connections - and more than 90% of these requests has no respond from the server.

My goal is to reduce requests from client to server, so i am trying to implement the same thing but on REVERSE order: every time there is an update on the server ( the server has a message for the client ) it sends it to the client - the client will have to listen listen periodically for the server for a message.

How can i imlement such mechanism?

Does it work?

If not, what is the best way to solve my problem?

Is it better that i implement the server side also in Java?

Upvotes: 0

Views: 2295

Answers (2)

marc
marc

Reputation: 1304

How about using the publisher/subscriber pattern as implemented in the distributed messaging library zeromq. There are bindings available for php and java. I've used zeromq recently in a project and was quite suprised by its ease of use and performance.

Upvotes: 0

mikeb
mikeb

Reputation: 11287

You can use websockets, which are designed for this: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

You can do other things, but that's probably the easiest.

Upvotes: 1

Related Questions