How to send message from server to client in web application
Asked Answered
W

2

8

Recently, I came across this kind of need. The client(web browser) requests for a task to be done asynchronously at the server side, then the client leaves it behind.

When the server finishes the task, it will notify the client by sending some message.

So, my question is:

Is there a way to send message(plain text, json, etc) from the server side to the client initiatively?

Whatsoever answered 21/2, 2012 at 7:20 Comment(1)
What are you trying to do? Some sort of scenario would be helpful. What server and client sides tools are you using? E.g. PHP (server) and Javascript (client). Or is it something different (e.g. Java, C, ...). Short answer: Yes it is possible. A more detailed answer requires some additional information :).Prolegomenon
V
9

Off the top of my head, two possible solutions come to mind:

The first, most common (and safest) solution to this will probably be AJAX long-polling, that is, setting up an ajax query on the client to periodically make a request and do something with the response. For example, setting up a resource that returns your "completed" message when the server side processing is complete. It's not ideal, given your question, but it is certainly feasible and pretty reliable.

To actually use push technology (which is precisely what you're asking, but not necessarily what you really need), you should take a look at WebSockets. If you take a look at that link you will see in the body of the article the list of browsers supporting the technology, and depending on your use case, you may or may not wish to investigate further.

I personally deal with a lot of old browsers so the ajax polling would be my go to guy. Assuming this is a semi long running process, polling every 2-5 seconds should not upset your users too much. This won't be good for real-time chats or anything, but for "I parsed your 200M excel file" it should be fine.

Visconti answered 21/2, 2012 at 7:39 Comment(1)
Just to clarify, if your example means the user actually leaves the page that would be polling (i.e. closes browser, goes to another site, etc) I can't think of anyway to persist that connection. If that is your example, an alternative notification system should probably be examined. (probably email)Visconti
J
7

There are three common techniques to solve this problem:

  1. XMLHttpRequest - Use the XMLHttpRequest object to make periodic requests from the client (web browser) to the web server to see if new information is available. This technique is commonly called polling. The disadvantage of polling is that the server cannot transmit the message until the client asks for it.

  2. Server-sent events - Use an EventSource object to create a callback that executes when the web server sends new messages. This technique is commonly called server push. The disadvantage of server push is that Internet Explorer and Edge do not currently support it, and the web server doesn't know if a client has stopped listening until the server attempts to send a message.

  3. WebSockets - Use a WebSocket object to create a two-way communication channel so the server can send a message to the client and vice versa at any time. This technique uses more network resources than the previous two methods and is more commonly used in a chat program where messages must be sent both directions immediately.

Jambalaya answered 20/11, 2019 at 18:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.