I'm using Html5 Server Sent Events. The server side is Java Servlet. I have a json array data wants to pass to server.
var source = new EventSource("../GetPointVal?id=100&jsondata=" + JSON.stringify(data));
If the array size is small , the server side can get the querystring. But if the array size is big. (maybe over thousands of characters), the server can't get the querystring. Is it possible to use POST method in new EventSource(...)
to to pass the json array to server that can avoid the querystring length limitation?
SSE is designed to use the JavaScript EventSource API in order to subscribe to a stream of data in any popular browser. Through this interface a client requests a particular URL in order to receive an event stream. SSE is commonly used to send message updates or continuous data streams to a browser client.
The server-side event stream syntax is simple. Set the "Content-Type" header to "text/event-stream". Now you can start sending event streams. header('Content-Type: text/event-stream');
Receive Server-Sent Event Notifications When an onmessage event occurs, put the received data into the element with id="result"
To use Server-Sent Events in a web application, you would need to add an <eventsource> element to the document. The src attribute of <eventsource> element should point to an URL which should provide a persistent HTTP connection that sends a data stream containing the events.
No, the SSE standard does not allow POST.
(For no technical reason, as far as I've been able to tell - I think it was just that the designers never saw the use cases: it is not just large data, but if you want to do a custom authentication scheme there are security reasons not to put the password in GET data.)
XMLHttpRequest
(i.e. AJAX) does allow POST, so one option is to go back to the older long-poll/comet methods. (My book, Data Push Apps with HTML5 SSE goes into quite some detail about how to do this.)
Another approach is to POST
all the data in beforehand, and store it in an HttpSession
, and then call the SSE process, which can make use of that session data. (SSE does support cookies, so the JSESSIONID
cookie should work fine.)
P.S. The standard doesn't explicitly say POST cannot be used. But, unlike XMLHttpRequest
, there is no parameter to specify the http method to use, and no way to specify the data you want to post.
While you cannot use the EventSource
API to do so, there is no technical reason why a server cannot implement for a POST request. The trick is getting the client to send the request. For instance This answer discusses sse.js as a drop in replacement for EventSource
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With