15

According to WHATWG - Server-Sent Events below is the API for using EventSource interface:

[Constructor(DOMString url, optional EventSourceInit eventSourceInitDict)]
interface EventSource : EventTarget {
  readonly attribute DOMString url;
  readonly attribute boolean withCredentials;
  //....
};

The withCredentials attribute must return the value to which it was last initialized. When the object is created, it must be initialized to false.

Simple example:

var stocks = new EventSource("events.php");
stocks.onmessage = function (event) {
  //alert(event.data);
};

Now, how to include or set withCredentials in this example?

Charles
  • 50,943
  • 13
  • 104
  • 142

1 Answers1

20

I've not tried it, but going by the spec you link to, I believe it would be like this:

var stocks = new EventSource("events.php", { withCredentials: true });

If you go to http://www.w3.org/TR/WebIDL/#idl-exceptions then scroll up to see the example immediately above that, you can see a similar pattern of using a dictionary to set initialization values.

Alohci
  • 78,296
  • 16
  • 112
  • 156
  • 1
    Its correct, I tried many syntax but Firebug not triggered any error with this :) –  Apr 28 '13 at 20:40
  • I Googled this pattern, I found also [w3c -> event-source -> eventsource-cross-origin.htm](https://github.com/w3c/event-source/blob/master/eventsource-cross-origin.htm) Using same concept –  Apr 28 '13 at 20:58
  • @user1646111 -- dead link; the new one is here: http://w3c-test.org/eventsource/eventsource-cross-origin.htm – Lars Kemmann Oct 13 '16 at 19:38
  • Adding the `, { withCredentials: true }` solved my problem – etoricky Mar 01 '21 at 19:01