How to Receive Notification Through Web Socket


Create SignalR hub listener to receive web socket notification

Anyone who wants to receive web socket notification needs to create listener to listen hub data and register user to the hub to receive notification.

Notification Hub Details

Hub URL : http://devext-signalr-core-us-e-001-wapp.azurewebsites.net/signalr

Hub Name : notificationHub


Method NameDescription
RegisterUser(string companyId, string accessToken)

This method will register user to the notification hub to receive data. Parameter described below:

companyId: Tenant guid to receive notification for particular tenant.

accessToken: User's access token after logged into tenant.

UnRegisterUser(string companyId, string accessToken)

This method will un-register user to the notification hub to stop receiving data. Parameter described below:

companyId: Tenant guid to receive notification for particular tenant.

accessToken: User's access token after logged in to tenant.

broadcastMessageThis method is used to receive data from web socket. It will return function with 2 parameters like "name" & "message" i.e.  function (name, message). In this name will be username and unique connection id combination and the message will be the json data in key value pair.

RegisterUserToHub(string cuId, string userEmail)

(For Notification Micro Service)

This method will register user to the notification hub to receive data. Parameter described below:

cuId: Tenant cuid to receive notification for particular tenant.

userEmail: Unique user email id.

Example of implementation in Asp.Net is as below:

  • Create new asp.net web application
  • Add nuget package Microsoft.AspNet.SignalR
  • Create view
    • Include jquery.js and signalr.js from scripts folder
    • Include signalr server url


<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.1.js"></script>
<script src="http://devpoc-signalr-core-us-e-001-wapp.azurewebsites.net/signalr/hubs"></script>


  • Create connection,register user and receive data


<script type="text/javascript">
$(document).ready(function () {
         $.connection.hub.url = "http://devpoc-signalr-core-us-e-001-wapp.azurewebsites.net/signalr";
//Starting hub
$.connection.hub.start()
         .done(function () { console.log("Now connected"); })
         .fail(function () { console.log('Could not connect'); });
//Getting hub connection
var hubConn = $.connection.notificationHub;
//To receive message. It will be the same for old notification api and notification micro service.
hubConn.client.broadcastMessage = function (name, message) {
         //Do your code
 };
$('#btnRegister').click(function () {
         //User register method
          hubConn.server.RegisterUser('companyId', 'accessToken')
                  .done(function (result) {
                           if (result) {
                                    //success
                           }
                  });
         });
});
$('#btnRegister').click(function () {
         //User register method to hub for notification micro service
          hubConn.server.RegisterUserToHub('cuId', 'userEmail')
                  .done(function (result) {
                           if (result) {
                                    //success
                           }
                  });
         });
});
$('#btnUnRegister').click(function () {
         //User unregister method
          hubConn.server.UnregisterUser('companyId', 'accessToken')
                  .done(function (result) {
                           if (result) {
                                    //success
                           }
                  });
         });
});
</script>