Search code examples
javascriptangularjshtmlcookieseventsource

EventSource with cookies using AngularJS


I've been looking at using EventSource to push server events to the client, but I need the client to be able to identify itself so it doesn't just get every event of every client. I am trying to use cookies to achieve this, but somehow the cookies never seem to come through. Can anyone point me to an example creating an EventSource and sending cookies with the request?


Solution

  • It sounds like a CORS issue. Try this (for dev only, not prod):

    On the client side you should have the withCredentials: true flag:

    const eventSource = new EventSource(subscribeUrl, { withCredentials: true });
    

    On the server side make sure you send those headers:

    Access-Control-Allow-Origin: YOUR_DOMAIN
    Access-Control-Allow-Credentials: true
    Access-Control-Expose-Headers: *