SSEvents - Problems closing the connection

We have a server side event connection.

We close the connection with this.

connection.close() 

It runs without an error, but seconds later we get another connection.

We read this post

https://forums.servicestack.net/t/sse-connecting-twice-if-session-terminated-at-server/8625/12

And found your comment that we should call

connection.close()     
$.ss.disposeServerEvents();

But then we see this

Error logged: TypeError: Cannot read property 'readyState' of null
at sendHeartbeat (ss-utils.js:728)
at eval (platform-implementation.js:47)
at Function.value (platform-implementation.js:47)
at eval (platform-implementation.js:47) 

Original error stack:
TypeError: Cannot read property 'readyState' of null
    at sendHeartbeat (chrome-extension://ljcdojknmlicdfoijbipailgnjfpmlmh/lib/ss-utils.js:728:54)

This is occurring in ss-utils.js:728

     if ($.ss.eventSource.readyState === 2) //CLOSED

We attempted to check for nulls at line 728, but then we had stack traces in other places.

Any ideas?

Mitch

What is connection in connection.close() ?

https://html.spec.whatwg.org/multipage/server-sent-events.html

eventsource.close()

You should only be calling $.ss.disposeServerEvents() which also closes the EventSource connection, not doing both.

we removed the close() and we still get errors.

It appears that this function is getting called after the $.ss.disposeServerEvents(); is getting executed. Thats why $.ss.eventSource == null .

The eventhandler for the sendHeart beat is called when eventsource == null.

   // ssutils.js  line 725
    function sendHeartbeat() 
    {
       if (connectId !== $.ss.CONNECT_ID) 
           return;
       if ($.ss.eventSource.readyState === 2) //CLOSED
       {
           var stopFn = $.ss.handlers["onStop"];
           if (stopFn != null)
          stopFn.apply($.ss.eventSource);
          $.ss.reconnectServerEvents({ errorArgs: { error:'CLOSED' } });
          return;
       }

I added a null check on line 726. It appears to work without throwing errors.

But i dont know if im breaking something else.

if ($.ss.eventSource == null) return;

Iā€™m also short-circuiting it in this commit. Will be available in the next ServiceStack v5.9.3+ on MyGet after it runs through CI or available now from: https://unpkg.com/ss-utils/ss-utils.js

1 Like