question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Server sent events not firing onclose/reconnect event on expiration of JWT

See original GitHub issue

Describe the bug

To test that our reconnection logic on the expiration of the Azure SignalR JWT worked correctly, I set the AccessTokenLifetime to 1 minute.

services.AddSignalR()
                .AddNewtonsoftJsonProtocol()
                .AddAzureSignalR(o =>
                {
                    o.AccessTokenLifetime = TimeSpan.FromMinutes(1);
                });
        

Whilst using WebSockets, as expected, there was no disconnection after 1 minute and the connection remained open for the duration of testing. Whilst using LongPolling, as soon as a 401 error is received from a long poll response, the onclose event is fired allowing us to manage a reconnection.

However, whilst using SSE, the occasional ping or attempt to post a message back to the Azure SignalR server results in a 401 error that does not appear to initiate an onclose or reconnect event. As such, we have no way to know that the communication between the server has now broken, and cannot force it to renegotiate.

This makes SSE functionally unusable with Azure SignalR as our clients will permanently disconnect after the 1 hour JWT timeout.

To Reproduce

As above, using a short JWT expiry and the following javascript connection:

new signalR.HubConnectionBuilder()
            .withUrl(connectionUrl,
                {
                    transport: signalR.HttpTransportType.ServerSentEvents,
                })
            .withAutomaticReconnect() //optional, but neither reconnection nor onclose are occurring
            .build();

Exceptions (if any)

The console fills with the following errors, however no attempt to reconnect trigger an ‘onclose’ or ‘onreconnecting’ event occurs.

image

With LongPolling, we receive a single error, followed by a reconnection (onclose without the withAutomaticReconnect())

Further technical details

  • Your Azure SignalR SDK version: Microsoft.Azure.SignalR@1.5.0
  • Your SignalR Client SDK version: Javascript - microsoft-signalr@3.1.3

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
BozeTadiccommented, Jul 8, 2022

Oh what version of the client library are you using?

The client library version is “^6.0.4” The change seems to be the renamed cachedPingMessage > _cachedPingMessage.

It works with the underscore. A simple oversight.

await connection.start()
await connection.sendMessage(_connection.cachedPingMessage);

Thank you for helping.

1reaction
vicancycommented, Jul 8, 2022

does not mimic the ping message correctly and sends a regular type 1 message with cachedPingMessage string value as content.

Oh what version of the client library are you using? I just did a quick test and it did work with json protocol and 5.0 js client

await connection.start()
await connection.sendMessage(connection.cachedPingMessage);
image
Read more comments on GitHub >

github_iconTop Results From Across the Web

Server Sent Event; `EventSource.onmessage` not firing
I'm trying to use the following example of Server Sent Events. Seems like the server is emitting the data, but the event is...
Read more >
Build Real-time Applications with ASP.NET Core SignalR
SignalR was set up to solve this problem and provide easy support for real-time capabilities on the ASP.NET stack by creating server- and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found