How To broadcast client events |
This article explains how an application can send client-side messages/events to its users.
First, the user page must subscribe to the application hub:
<script> $(document).ready(function () { PF_SubscribeToApplicationHub("MyApplication"); }); </script>
Then, you can listen to broadcasted events:
<script> $(document).ready(function () { PF_SubscribeToApplicationHub("MyApplication"); $(pageApplicationEvents).on('MyCustomEvent', function (event, param1) { console.log("MyCustomEvent triggered:" + param1); }); $(pageApplicationEvents).on('AnotherCustomEvent', function (event, param1, param2) { console.log("AnotherCustomEvent triggered."); }); }); </script>
The pageApplicationEvents object is global and provided by Packflow.
Note |
---|
This feature is based on the SignalR library, the broadcast will only be effective from the w3wp (IIS) process. E.g. the event will not be triggered if the broadcast occured in a timer job. For that reason, we recommend to broadcast events from web components: MVC, Pages, Parts, Web services, Application hub. |
The following example starts a broadcast to the current user from a page part:
MyApplicationHub.Broadcast(this.CurrentApplication, this.CurrentUser, "MyCustomEvent", "New item created");
In this static call, we find the following parameters:
Other overloads allow to broadcast to multiple users, or to choose among the subscribers of the PFApplicationHubCaller.Subscribers collection.