SSE with Redis cache is not recovering from connection error

Firstly you shouldn’t be changing the default to ReuseScope.Request which is the most unpredictable scope requiring different backing implementations in different hosts, inc. test projects. It’s also less efficient for pooled resources, it obviously should never be used for singleton instances and as it’s also more efficient to store any per-request data in IRequest.Items, there’s basically no reason to use Request scope, it definitely shouldn’t be the default which is going to conflict with all the docs relying on the default singleton scope.

//Container.DefaultReuse = ReuseScope.Request; // never do this

E.g. the RedisManagerPool and RedisServerEvents are singleton dependencies that are required to be registered as singletons.

Next you need to use one of our Server Event Clients which issues periodic heartbeats and include auto connection retry when it detects a failed SSE connection. For web pages you can either use the ss-utils.js JavaScript client or TypeScript Client.

As you’re already using jQuery it’s easiest to use ss-utils.js.

Please see Run side-by-side with another Framework for how to properly host ServiceStack on a custom path, copy the Web.config section from the above docs and make sure to specify the custom path in HandlerFactoryPath:

public override void Configure(Container container)
{
    SetConfig(new HostConfig { HandlerFactoryPath = "api" });
}

After you’ve updated your config you can reference the built-in ss-utils.js like:

<script src="/api/js/ss-utils.js"></script>
<script src="/js/default.js"></script>

Your subscribe() function is unnecessary as the channels are subscribed to when establishing the SSE connection so change it to just initializing the SSE connection instead:

<body onload="initSSE();">

Then rewrite it to use ss-utils.js to manage the connection, e.g:

function initSSE() {
    if (!eventSource) {
        try {
            eventSource = new EventSource('/api/event-stream?channels=mychannel&t=' + new Date().getTime());
            $(eventSource).handleServerEvents({
                handlers: {
                    onConnect: function(e) {
                        console.log('onConnect',e);
                    },
                    onMessage: function (e) {
                        console.log('onMessage',e);
                        addMessage(e);
                    }
                }
            });
        }
        catch(ex)
        {
            addMessage("ERROR: initSSE");
            addMessage("ERROR: " + ex);
        }
    }
    return true;
}

I’ve also changed your addMessage() to output serialize object as JSON when it’s not logging a string:

function addMessage(message) {
    if (typeof message != 'string')
        message = JSON.stringify(message);
//...
}

I did detect an unhandled failure connection in the underlying RedisPubSubServer Redis SSE uses which should now be resolved in the latest v5.8.1 that’s now available on MyGet.

Please test with the latest v5.8.1 and let me know if you’re still able to repro the issue.