Documentation

Push and Pull

Module allows to organize transport to send instant commands. Implementation of this feature as a separate module allows for any module (including modules of the third party developers) to send instant motifications and messages to clients via API.

Connecting the module:

if (!CModule::IncludeModule('pull'))
   return false;

Also, module dependency must be registered for the Push and Pull module. Registration of dependency handler:

RegisterModuleDependences("pull", "OnGetDependentModule", "your_module", "CYourModulePullSchema", "OnGetDependentModule" );

Events and push-notifications are sent to list servers in the page epilogue. If you send events in ajax-handlers, you need to use CMain::FinalActions() at the handler's finish.

Example of the class code

class CYourModulePullSchema
{
    public static function OnGetDependentModule()
    {
        return Array(
            'MODULE_ID' => "your_module",
            'USE' => Array("PUBLIC_SECTION")
     );
     }
}

To handle the commands sent via PHP the following JS code must be used in the layout:

Trap for desktop app pages (all events, except online events):

BX.addCustomEvent("onPullEvent-moduleName", BX.delegate(function(command,params){
   console.log('Events of moduleName', command, params);
}, this));

Trap for mobile app pages for module app pages (all events, except online events):

BX.addCustomEvent("onPull-moduleName", BX.delegate(function(data){
    console.log('Events of moduleName', data.command, data.params);
}, this));

Note: don't forget to change the name of your module in the "moduleName" example.

Example of code to handle PHP classes of the module (Pull, Pull Shared, Pull Watch):

BX.addCustomEvent("onPullEvent-main", function(module_id,command,params) {
    if (command == 'check')
    {
        console.log('Command from module MAIN - its work!');
    }
});

In the example we are subscribing to the command receiving event (onPullEvent-moduleName). ModuleName - is the name of your module, for example - main. Then we receive 'command' and 'params' in the function that we specified when sending a command via PHP. After that, we are processing our commands according to our own logic.

If your logic requires collecting all events, then the format will be a little different: (available on any version of pull)

Trap for desktop app pages (all events except online events):

BX.addCustomEvent("onPullEvent", BX.delegate(function(module_id,command,params){
   console.log(module_id, command, params);
}, this));

Trap for mobile app pages (all events except online events):

BX.addCustomEvent("onPull", BX.delegate(function(data){
    console.log(data.module_id, data.command, data.params);
}, this));

Example of code to handle PHP classes of the module (Pull, Pull Shared, Pull Watch):

BX.addCustomEvent("onPullEvent", function(module_id,command,params) {
    if (module_id == "test" && command == 'check')
    {
        console.log('Work!');
    }
});

In this example we are subscribing to the command receiving event (onPullEvent) and receive module_id command, params in the function, which we have specified when sending commands via PHP. Then we process our commands with account of your logic.

Notes:
  • To retrieve data about online events, use the onPullOnlineEvent event.
  • It's preferable to use event handlers for specific modules, instead on a handler for all events. Such format will be more efficient.



© «Bitrix24», 2001-2022
Up