GithubHelp home page GithubHelp logo

Comments (8)

hypesystem avatar hypesystem commented on August 23, 2024

@ghominejad My recommended approach would be to make Sender#send recursive. If we solve this problem, it will solve the one we have discussed extensively in #92, too.

from node-gcm.

hypesystem avatar hypesystem commented on August 23, 2024

I have started work on this branch: https://github.com/ToothlessGear/node-gcm/tree/ensure-full-result-set

from node-gcm.

ghominejad avatar ghominejad commented on August 23, 2024

I've fixed the problem compatible with my scenario last night but it needs to be tested more. It also can handle unlimited messages and high performance db operations. for example when you need to remove an id within million records you can find it with a database id and remove it.

var message = new gcm.Message({
    delayWhileIdle: true,
    timeToLive: 86400, // in seconds = 1 day
    data: data
});

// some devices have registerId and some deviceId
//   loaded from mongodb database
var devices = [
   {_id:'mongodb-id-1', registerId: 1},
   {_id:'mongodb-id-2', registerId: 2},
   {_id:'mongodb-id-3', deviceId: 'android-device-id-1 ....'},
   {_id:'mongodb-id-4', deviceId: 'android-device-id-2 ....'}
];

gcm.sendAll(message, devices, function (item) {
    if (item.removed) { return undefined;}
    if (item.registerId) { return item.registerId;}
    if (item.deviceId) { return item.deviceId;}

}).on('NotRegistered, InvalidRegistration' , function (item, result) {
    // remove from db
    item.removed = true;
    db.removeDevice(item._id , function (err){
        console.log('removed from db successed');
    });

}).on('canonicalId', function (item, result) {
    item.registerId = result.register_id;
    db.updateCanonicalId(item , function (err){
        console.log('canonicalId, updated : regiId : ' + item.registerId);
    });

}).on('finished', function () {
    console.log('all have sent');

}).on('Unavailable', function (unavailableItems) {
    console.log('unavailables after retries : ' + unavailableItems.length);
});

from node-gcm.

ghominejad avatar ghominejad commented on August 23, 2024

I think these should solve the problems but i haven't tested it. in this scenario we don't keep the order of the results but we store all results containing unavailables for the callback.

function sleepTime(attempt) {
    sleepTime = Math.pow (2, attempt) * backoff;
    if (sleepTime > Constants.MAX_BACKOFF_DELAY) {
        sleepTime = Constants.MAX_BACKOFF_DELAY;
    }
}
// the order of results is different with registrationIds
var allResults = [], unsentRegIds = [];

this.sendNoRetry(message, registrationIds, function lambda(err, result) {

    if (attempt >= retries) {  
        // we can't do more
        callback(null, allResults.concat(result.results));
        return; 
    } 

    var i, id, r; // r for each result.results

    // if we err'd resend them all
    if (err) {
        unsentRegIds = registrationIds;
    } 
    // success but check for bad ids
    else {
        unsentRegIds = []; // empty 
        for (i = 0; i < registrationIds.length; i += 1) {
            r = result.results[i];
            id = registrationIds[i];

            if (r.error === 'Unavailable') {
                unsentRegIds.push(id);
            } else {
                r.id = id; // users may need the id for updating with canonical ids
                allResults.push(r);
            }
        }
    }

    if (unsentRegIds.length) {
        // retry but with less items
        registrationIds = unsentRegIds;
        setTimeout(function () {
            self.sendNoRetry(message, registrationIds, lambda);
        }, sleepTime(attempt++));

    } else {
        // No more registration ids to send
        callback(null, allResults);
    }
});

from node-gcm.

hypesystem avatar hypesystem commented on August 23, 2024

You add an interesting concept: you tie the registration id to the result, so it is easier to figure out for users (nice, but ultimately unnecessary if the order is maintained correctly as per my PR #95). This might be interesting in a future release, but I would also like to maintain the order (which you don't).

from node-gcm.

ghominejad avatar ghominejad commented on August 23, 2024

Of course it's bad solution. why don't keep the position of each unsent item?

unsents = [{pos:0, id:1}, {pos:1, id:2}];

And after the response, mach each result with unsents and update allResults.
unsents can be narrower at each new retries with holding same positions inside it.

from node-gcm.

hypesystem avatar hypesystem commented on August 23, 2024

@ghominejad please try and look at my PR: #95 in which I already do this, but by making send recursive.

from node-gcm.

ghominejad avatar ghominejad commented on August 23, 2024

You are very intelligent. good codes. thank you

from node-gcm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.