Azure SignalR Service now supports Event Grid! - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: C#, Visual Basic, & .Net Frameworks (https://www.sickgaming.net/forum-79.html) +--- Thread: Azure SignalR Service now supports Event Grid! (/thread-91925.html) |
Azure SignalR Service now supports Event Grid! - xSicKxBot - 09-10-2019 Azure SignalR Service now supports Event Grid! <div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/azure-signalr-service-now-supports-event-grid.png" width="58" height="58" title="" alt="" /></div><div><div class="row justify-content-center"> <div class="col-md-4"> <div><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/azure-signalr-service-now-supports-event-grid.png" width="58" height="58" alt="Avatar" class="avatar avatar-58 wp-user-avatar wp-user-avatar-58 photo avatar-default"></p> <p>Ken</p> </div> </div> </div> <div class="entry-meta"> <p>August 8th, 2019</p> </p></div> <p><!-- .entry-meta --> </p> <p>Since we GA’ed Azure SignalR Service in last September, serverless has become a very popular use case in Azure SignalR Service and is used by many customers. Unlike the traditional SignalR application which requires a server to host the hub, in serverless scenario no server is needed, instead you can directly send messages to clients through REST APIs or our management SDK which can easily be used in serverless code like Azure Functions.</p> <p>Though there is a huge benefit which saves you the cost of maintaining the app server, the feature set in serverless scenario is limited. Since there is no real hub, it’s not possible to respond to client activities like client invocations or connection events. Without client events serverless use cases will be limited and we hear a lot of customers asking about this support. Today we’re excited to announce a new feature that enables Azure SignalR Service to publish client events to Azure Event Grid so that you can subscribe and respond to them.</p> <h2>How does it work?</h2> <p>Let’s first revisit how serverless scenario in Azure SignalR Service works.</p> <ol> <li> <p>In serverless scenario, even you don’t have an app server, you still need to have a negotiate API so SignalR client can do the negotiation to get the url to SignalR service and a corresponding access token. Usually this can be done using an Azure Function.</p> </li> <li> <p>Client will then use the url and access token to connect to SignalR service.</p> </li> <li> <p>After clients are connected, you can send message to clients using <a href="https://docs.microsoft.com/en-us/azure/azure-signalr/signalr-quickstart-rest-api">REST APIs</a> or <a href="https://github.com/Azure/azure-signalr/blob/dev/docs/management-sdk-guide.md">service management SDK</a>. If you are using Azure Functions, our <a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-signalr-service">SignalR Service binding</a> does the work for you so you only need to return the messages as an output binding.</p> </li> </ol> <p>This flow is illustrated as step 1-3 in the diagram below:</p> <p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/azure-signalr-service-now-supports-event-grid-1.png" alt="Serverless workflow" width="653" height="597" class="alignnone size-full wp-image-22372"></p> <p>What’s missing here is that there is no equivalent of <code>OnConnected()</code> and <code>OnDisconnected()</code> in serverless APIs so there is no way for the Azure function to know whether a client is connected or disconnected.</p> <p>Now with Event Grid you’ll be able to get such events through an Event Grid subscription (as step 4 and 5 in the above diagram):</p> <ol> <li> <p>When a client is connected/disconnected to SignalR service, service will publish this event to Event Grid.</p> </li> <li> <p>In Azure function you can have an Event Grid trigger and subscribe to such events, then Event Grid will send those events to the function (through a webhook).</p> </li> </ol> <h2>How to use it?</h2> <p>It’s very simple to make your serverless application subscribe to SignalR connection events. Let’s use Azure function as an example.</p> <ol> <li> <p>First you need to make sure your SignalR Service instance is in serverless mode. (<a href="https://ms.portal.azure.com/#create/Microsoft.SignalRGalleryPackage">Create a SignalR Service instance</a> if you haven’t done so.)</p> <p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/azure-signalr-service-now-supports-event-grid-2.png" alt="Enable serverless mode" width="514" height="458" class="alignnone size-full wp-image-22373"></p> </li> <li> <p>Create an Event Grid trigger in your function app.</p> <p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/azure-signalr-service-now-supports-event-grid-3.png" alt="Create Event Grid trigger" width="727" height="614" class="alignnone size-full wp-image-22369"></p> </li> <li> <p>In the Event Grid trigger, add an Event Grid subscription.</p> <p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/azure-signalr-service-now-supports-event-grid-4.png" alt="Add Event Grid Subscription" width="554" height="152" class="alignnone size-full wp-image-22368"></p> <p>Then select your SignalR Service instance.</p> <p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/azure-signalr-service-now-supports-event-grid-5.png" alt="Select SignalR Service instance" width="750" height="714" class="alignnone size-full wp-image-22371"></p> </li> </ol> <p>Now you’re all set! Your function app is now able to get connection events from SignalR Service.</p> <p>To test it, you just need to open a SignalR connection to the service. You can use the SignalR client in our sample repo, which contains a simple negotiate API implementation.</p> <ol> <li> <p>Clone <a href="https://github.com/aspnet/AzureSignalR-samples">AzureSignalR-samples</a> repo.</p> </li> <li> <p>Start the sample negotiation server.</p> <pre><code>cd samples\Management\NegotiationServer set Azure__SignalR__ConnectionString=<connection_string> dotnet run </code></pre> </li> <li> <p>Run SignalR client.</p> <pre><code>cd samples\Management\SignalRClient dotnet run </code></pre> <p>Open the function logs in Azure portal and you’ll see a connected event is sent to the function:</p> <p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/azure-signalr-service-now-supports-event-grid-6.png" alt="Azure Function output" width="742" height="398" class="alignnone size-full wp-image-22370"></p> <p>If you stop the client you’ll also see a disconnected event is received.</p> </li> </ol> <h2>Try it now!</h2> <p>This feature is now in public preview so feel free to try it out and let us know your feedback by filing issues on <a href="https://github.com/azure/azure-signalr/issues">Github</a>.</p> <p>For more information about how to use Event Grid with SignalR Service, you can read this <a href="https://docs.microsoft.com/en-us/azure/azure-signalr/signalr-howto-event-grid-integration">article</a> or try this <a href="https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/EventGridIntegration">sample</a>.</p> <div class="authorinfoarea"> <div><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/azure-signalr-service-now-supports-event-grid-7.png" width="96" height="96" alt="Avatar" class="avatar avatar-96 wp-user-avatar wp-user-avatar-96 photo avatar-default"></div> <div> <h5><a class="no-underline" aria-label="Ken Chen" target="_blank" href="https://devblogs.microsoft.com/aspnet/author/kenchenmicrosoft-com/" rel="noopener noreferrer">Ken Chen</a></h5> <p>Principal Software Engineering Manager</p> <p><strong>Follow Ken</strong> <a class="no-underline stayinformed" aria-label="Ken Chen Twitter profile" target="_blank" href="https://twitter.com/chenkennt" rel="noopener noreferrer"><i class="fa fa-twitter"></i></a><a class="no-underline stayinformed hvr-pop" aria-label="Ken Chen RSS Feed" target="_blank" href="https://devblogs.microsoft.com/aspnet/author/kenchenmicrosoft-com/feed/" rel="noopener noreferrer"></a></p> </p></div> </p></div> </div> https://www.sickgaming.net/blog/2019/08/09/azure-signalr-service-now-supports-event-grid/ |