Todor Yanev
Todor Yanev

Reputation: 85

Can the Server tell the Client to refresh the page?

I have a Client that tell my server to contact another server and save the data there upon saving if successful I can receive a callback, how can I then tell the Client to refresh the page because this 2nd Server is also sending data to the Client, can I do it using headers?

Upvotes: 1

Views: 2034

Answers (1)

obfish
obfish

Reputation: 667

There is no tranditional ways to push messages (refresh request and so on) to the webpage directly from the server.

Before giving some solutions, I'm sorry to say that your description to your question is quite ambiguous. server, another server, this 2nd Server is also sending data to the Client does not make much sense. You may reorganize your description to show the whole business logic better. Giving necessary code will be better.

So focusing on sending message from server to client within browser environment, there are several ways you could consider:

Ajax

You could use ajax to request the server at client side. You can poll the server at regular intervals, checking the response to determine whether the page should be refreshed.

Pro: widely supported, easy for both client side and server side

Con: polling is not a real-time solution and will make some redundant requests

Websocket

Pro: real-time bidirectional socket-like communication

Con: may be too heavy for the simple task you mentioned

Server push

Part of PWA specification

Pro: allow direct communication from server to client

Con: complexity, insufficient browser support

Upvotes: 4

Related Questions