Comments (8)
@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.
I have started work on this branch: https://github.com/ToothlessGear/node-gcm/tree/ensure-full-result-set
from node-gcm.
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.
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.
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.
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.
@ghominejad please try and look at my PR: #95 in which I already do this, but by making send
recursive.
from node-gcm.
You are very intelligent. good codes. thank you
from node-gcm.
Related Issues (20)
- Address deprecation of request package HOT 2
- How to add analytics label to message? HOT 5
- Unable to send push to browser(firefox, chrome) always getting InvalidRegistration HOT 4
- Make send URI configurable HOT 1
- fcm_options and analytics_label fields are not sent to FCM servers? HOT 2
- TypeError: Cannot read property 'error' of undefined failed_tokens example HOT 1
- Not receiving notifications - ios HOT 2
- Add snyk to fix vulnerabilities faster HOT 15
- Add missing GitHub releases and version tags
- Add CI to require passing tests before release HOT 2
- didReceiveRemoteNotification:fetchCompletionHandler not called. HOT 6
- readme error in failedTokens snippets HOT 1
- Removing no longer valid FCM tokens HOT 1
- Add release automation HOT 2
- Support for Google Service Accounts authorization HOT 2
- Firebase Legacy HTTP API discontinued by June 20, 2024 HOT 12
- request package security issue. HOT 3
- Handle non-HTTP errors HOT 1
- Handle all HTTP errors
- Migration from legacy FCM to HTTP v1 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-gcm.