Search code examples
javaserverlong-polling

How can I make a server push


I developed my own client and server in java und I implemented a long-polling communication. So my client will always ask for new operations to be executed. If a server has a new operation, then he will send it to the client. The whole communication is based on java-sockets. Now I am thinking to renounce on long-polling-communication and implement a server-push. In this case the client will not ask my server anymore. He will just wait for notifications. If my server has a new operation to be executed, then he will make a server-push. My Goal is to save time on the client-side. So he will not send any request anymore

Question1: is it possible to implement a server-push on my model based on sockets in Java ?

Question2: if yes,how can I implement a server-push?


Solution

  • On server side:

    ...    
    DataOutputStream toClient = new DataOutputStream(socket.getOutputStream()); 
    outToClient.writeBytes("event_xyz");
    ...
    

    On client side you should listen for events