1

I have the following scenario: A user clicks on a button in the settings .
This button will do a resource get call which calls an API.
This API will do nothing but some processing.
It will make a call to an ERP system and then process that result into a database.

Now I've managed to achieve all of the above.
However, I wish to have a live progress bar which will display the state of the processing.
Now I've googled a lot on how to achieve this but sadly I haven't found anything yet.

Could anyone help me in the right way?

**** EDIT ****

I'll try to give some more details.

I have a button in a Vue

<button type="button" class="btn btn-space btn-primary md-trigger" v-on:click="syncItemGroups();">{{ $t('titles.erp.sync_item_groups') }}</button>

The onclick event calls the Vue method syncItemGroups() which contains the following code:

syncItemGroups() {
    var _this = this;
    this.$dispatch('add-global-loader');
    _this.erpItemGroups = [];
    this.syncItemGroupsDataSource.get().then(function (response) {
        _this.erpItemGroups = response.data.erpItemGroups;
        _this.erpItemGroupsCount = _this.erpItemGroups.length;
        _this.$dispatch('remove-global-loader');
    });
}

The API that is being called contains the following code:

public function itemGroups()
{
    $tenantId    = $this->getCurrentTenant()->id;
    $integration = TenantIntegration::where('type', '=', 'erp')->where('tenant_id', '=', $tenantId)->get()->first();

    $this->getErpProvider()->syncItemGroups($integration->id, $tenantId);

    $itemGroups = ErpItemGroup::whereTenantId($tenantId)->get();

    return [
        'success'       => true,
        'erpItemGroups' => $itemGroups
    ];
}

Within this method the syncItemGroups is being called. This contains a foreach loop which does the processing. Now what i wish to achieve is that within this foreach loop the progress of this loop is being reported back to the website to populate a progress bar.

public function syncItemGroups($integrationId, $tenantId)
{

    $connection = $this->connect($integrationId);
    $connection->setDivision(1617429);
    $itemGroups = new ItemGroup($connection);
    $result     = $itemGroups->get();



    foreach ($result as $itemGroup) {
        $existingErpItemGroup = ErpItemGroup::where([
            ['tenant_id', $tenantId],
            ['item_group_id', $itemGroup->Code],
        ])->get()->first();
        if (is_null($existingErpItemGroup) ||
            $existingErpItemGroup->item_group_id !== $itemGroup->Code ||
            $existingErpItemGroup->description !== $itemGroup->Description) {
            $erpItemGroup                =
                is_null($existingErpItemGroup) ? new ErpItemGroup() : $existingErpItemGroup;
            $erpItemGroup->tenant_id     = $tenantId;
            $erpItemGroup->item_group_id = $itemGroup->Code;
            $erpItemGroup->description   = $itemGroup->Description;
            $erpItemGroup->save();
        }
    }
}

Hopefully it's clear now what i wish to achieve.

Thanks in advance.

Kind regards, Pim Dröge

Pim Dröge
  • 85
  • 1
  • 10
  • How are you making the call? using AJAX, CURL etc. etc.? – Finlay Roelofs Sep 21 '17 at 17:43
  • @FinlayRoelofs I've edited the question with some code. – Pim Dröge Sep 21 '17 at 17:49
  • 1
    You will need to expose the state of your processing somehow. One way is to have the API call immediately return with a progress URL, which you can then periodically check for the state of the processing. – Botje Sep 21 '17 at 18:21
  • As @Botje suggests, this seems like a good approach. You could use [long polling](https://stackoverflow.com/questions/18560872/how-does-long-polling-work-javascript) or a [pub/sub](https://laravel.com/docs/5.5/broadcasting) approach. – tptcat Sep 22 '17 at 13:01

0 Answers0