Start a new topic

Live Service with Kinvey Studio

I need to get the Live Service to work with Kinvey Studio.

I have been following the guide on https://devcenter.kinvey.com/nativescript/guides/live-service and it works well all the way to "Subscribing to a Collection".


How and where should I implement the subscription to the collection?


I guess that I shouldn't use a Datastore as they do in the example. I guess the Kinvey Studio framework gives me access to the collection and I can set up to the subscription.


It would be very good to have a "best practice" list on how to set this up to work with a Kinvey Studio project.


//Thomas


1 person has this question

Hello Thomas,


I would like to apologize for taking so long to respond to your question.


From your comment I see that you have already taken the first steps and enabled the Live Service for your application. Below you could find a basic code example on how to achieve this. In my example, I am modifying the content of a list view that contains vehicles (to work for you, you need to provide the name of your collection) by changing the code base of the <view-name>.components.tns.ts file:


import { Inject, Injector } from '@angular/core';
import { VehiclesViewBaseComponent } from '@src/app/modules/vehicles/vehicles/vehicles.base.component';

import { KinveyService } from '@src/app/core/data/kinvey.service';
import * as Kinvey from 'kinvey-nativescript-sdk';


export class VehiclesViewComponent extends VehiclesViewBaseComponent {
    private coreService: KinveyService;

    constructor(@Inject(Injector) injector: Injector) {
        super(injector);
        this.coreService = this.injector.get(KinveyService);     
    }

    ngOnInit() {
        const activeUser = Kinvey.User.getActiveUser();
        const vehicles = Kinvey.DataStore.collection('vehicles', Kinvey.DataStoreType.Network);

        activeUser.registerForLiveService()
        .then((res) => {

            return vehicles.subscribe({
                onMessage: (m) => {
                    this.coreService.getDataReload('vehicles').next();
                },
                onStatus: (s) => {
                    // handle status events, which pertain to this collection
                },
                onError: (e) => {
                    // handle errors
                }
              })
                .then((res) => {
                })
                .catch();
        })
        .catch(err => {
            console.log(err);
        });
    }
}


In addition to the ngOnInit function, you can find a couple of imports (kinvey.service and kinvey-nativescript-sdk) and a reference to the coreService (from kinvey.service), which gives you a way to reload the data and imports  


Important: In the latest version of Kinvey Studio, newly created apps are using kinvey-nativescript-sdk version 4.2.1, which exhibit some issues with the Live Service. In my example, I am using kinvey-nativescript-sdk version 4.2.3, which is working as expected. With this in mind, I recommend that you use the latest version of the above-mentioned SDK.


I hope this information is of any help.


Regards,

Garo

Hi Team,


Is there any way to subscribe to only particular row as we do not want to push anything changed on a collection to all users, We want to push the changes which is related to respective user.


So, is it possible to subscribe to a collection based on "_id" ? 


Or is there any way to publish to live services by writing custom code. 


I tried writing the flex service but but while publishing using pubnub sdk I am getting below error publishing{\"error\":true,\"operation\":\"PNPublishOperation\",\"statusCode\":403,


Regards

Ayush

Login or Signup to post a comment