How to Receive Notification Through Web Socket
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. Hub URL : http://devext-signalr-core-us-e-001-wapp.azurewebsites.net/signalr Hub Name : notificationHub 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. 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. 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 SignalR hub listener to receive web socket notification
Method Name Description RegisterUser(string companyId, string accessToken) UnRegisterUser(string companyId, string accessToken) broadcastMessage This 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. <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>
<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>