{"id":98143,"date":"2019-08-09T06:41:06","date_gmt":"2019-08-09T06:41:06","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/aspnet\/?p=22298"},"modified":"2019-08-09T06:41:06","modified_gmt":"2019-08-09T06:41:06","slug":"azure-signalr-service-now-supports-event-grid","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2019\/08\/09\/azure-signalr-service-now-supports-event-grid\/","title":{"rendered":"Azure SignalR Service now supports Event Grid!"},"content":{"rendered":"<div class=\"row justify-content-center\">\n<div class=\"col-md-4\">\n<div><img loading=\"lazy\" decoding=\"async\" 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>\n<p>Ken<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"entry-meta\">\n<p>August 8th, 2019<\/p>\n<\/p><\/div>\n<p><!-- .entry-meta --> <\/p>\n<p>Since we GA\u2019ed 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>\n<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\u2019s 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\u2019re 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>\n<h2>How does it work?<\/h2>\n<p>Let\u2019s first revisit how serverless scenario in Azure SignalR Service works.<\/p>\n<ol>\n<li>\n<p>In serverless scenario, even you don\u2019t 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>\n<\/li>\n<li>\n<p>Client will then use the url and access token to connect to SignalR service.<\/p>\n<\/li>\n<li>\n<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>\n<\/li>\n<\/ol>\n<p>This flow is illustrated as step 1-3 in the diagram below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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>\n<p>What\u2019s 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>\n<p>Now with Event Grid you\u2019ll be able to get such events through an Event Grid subscription (as step 4 and 5 in the above diagram):<\/p>\n<ol>\n<li>\n<p>When a client is connected\/disconnected to SignalR service, service will publish this event to Event Grid.<\/p>\n<\/li>\n<li>\n<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>\n<\/li>\n<\/ol>\n<h2>How to use it?<\/h2>\n<p>It\u2019s very simple to make your serverless application subscribe to SignalR connection events. Let\u2019s use Azure function as an example.<\/p>\n<ol>\n<li>\n<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\u2019t done so.)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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>\n<\/li>\n<li>\n<p>Create an Event Grid trigger in your function app.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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>\n<\/li>\n<li>\n<p>In the Event Grid trigger, add an Event Grid subscription.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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>\n<p>Then select your SignalR Service instance.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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>\n<\/li>\n<\/ol>\n<p>Now you\u2019re all set! Your function app is now able to get connection events from SignalR Service.<\/p>\n<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>\n<ol>\n<li>\n<p>Clone <a href=\"https:\/\/github.com\/aspnet\/AzureSignalR-samples\">AzureSignalR-samples<\/a> repo.<\/p>\n<\/li>\n<li>\n<p>Start the sample negotiation server.<\/p>\n<pre><code>cd samples\\Management\\NegotiationServer\nset Azure__SignalR__ConnectionString=&lt;connection_string&gt;\ndotnet run\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Run SignalR client.<\/p>\n<pre><code>cd samples\\Management\\SignalRClient\ndotnet run\n<\/code><\/pre>\n<p>Open the function logs in Azure portal and you\u2019ll see a connected event is sent to the function:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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>\n<p>If you stop the client you\u2019ll also see a disconnected event is received.<\/p>\n<\/li>\n<\/ol>\n<h2>Try it now!<\/h2>\n<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>\n<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>\n<div class=\"authorinfoarea\">\n<div><img loading=\"lazy\" decoding=\"async\" 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>\n<div>\n<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>\n<p>Principal Software Engineering Manager<\/p>\n<p><strong>Follow Ken<\/strong>&nbsp;&nbsp;&nbsp;<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>\n<\/p><\/div>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Ken August 8th, 2019 Since we GA\u2019ed 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":98144,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[54,105,355],"class_list":["post-98143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-webdev","tag-azure","tag-serverless","tag-signalr"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/98143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=98143"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/98143\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/98144"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=98143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=98143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=98143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}