Click or drag to resize

How To broadcast client events

This article explains how an application can send client-side messages/events to its users.

Prepare client-side

First, the user page must subscribe to the application hub:

JavaScript
<script>
    $(document).ready(function () {
        PF_SubscribeToApplicationHub("MyApplication");
    });
</script>

Then, you can listen to broadcasted events:

JavaScript
<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.

Call event server-side
Note 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:

C#
MyApplicationHub.Broadcast(this.CurrentApplication, this.CurrentUser, "MyCustomEvent", "New item created");

In this static call, we find the following parameters:

  • The application object.
  • The user object. (there are other possibilities, see below)
  • The event name.
  • The list of parameters received by the JavaScript handler.

Other overloads allow to broadcast to multiple users, or to choose among the subscribers of the PFApplicationHubCaller.Subscribers collection.