Search code examples
javaphpserverclientlistener

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?


Solution

  • 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.