-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
190 lines (175 loc) · 6.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
$ NODE_ENV=development node .
$ NODE_ENV=production node .
*/
var _ = require('lodash')
, notify = require('push-notify')
, apn = require('apn')
, redis = require("redis")
, util = require('util')
, RedisLockingWorker = require("redis-locking-worker")
, messages = require("./lib/messages")
, config = require("./config");
//
// REDIS MAIN CLIENT
//
var redisClient = redis.createClient(config.redis.port, config.redis.host);
if (config.redis.pass)
redisClient.auth(config.redis.pass);
redisClient.on('error', function (err) {
console.error("[redis-client] " + err);
}).on('connect', function () {
console.log("[redis-client] Connected");
}).on('close', function (why) {
console.log("[redis-client] " + why);
});
//
// REDIS SUBSCRIPTION CLIENT
//
var redisSubscriber = redis.createClient(config.redis.port, config.redis.host);
if (config.redis.pass)
redisSubscriber.auth(config.redis.pass);
redisSubscriber.on('error', function (err) {
console.error("[redis-subscriber] " + err);
}).on('connect', function () {
console.log("[redis-subscriber] Connected");
redisSubscriber.subscribe(config.redis.channels, function () {});
}).on('close', function (why) {
console.log("[redis-subscriber] " + why);
}).on('subscribe', function (channel, count) {
console.log("[redis-subscriber] Subscribed to '%s', %d total subscriptions", channel, count);
}).on('unsubscribe', function (channel, count) {
console.log("[redis-subscriber] Unsubscribed from '%s', %d total subscriptions", channel, count);
if (count === 0) {
redisSubscriber.end();
}
}).on('message', function (channel, message) {
console.log("[redis-subscriber] Received message: '%s'", message);
// Error handling elided for brevity
var msg = buildMessageBasedOnChannel(channel, message);
if (msg) {
var compiled = msg.compile();
if (compiled)
processMessage(msg);
else
console.warn('Invalid message?');
}
});
function buildMessageBasedOnChannel (channel, message) {
var chan = channel.toLowerCase();
if (chan.indexOf('gcm') != -1 || chan.indexOf('android') != -1)
return new messages.GCMMessage(message);
if (chan.indexOf('apns') != -1 || chan.indexOf('ios') != -1 || chan.indexOf('iphone') != -1 || chan.indexOf('ipad') != -1)
return new messages.APNSMessage(message);
return undefined;
}
//
// APNS GATEWAY
//
var apnsGateway = new notify.apn.Sender(config.apns.gateway.options);
apnsGateway.on('error', function (err) {
console.error("[apns-gateway] " + err);
}).on('socketError', function (err) {
console.error("[apns-gateway] " + err);
}).on('timeout', function (err) {
console.error("[apns-gateway] Timeout");
}).on('transmissionError', function (errCode, notification, device) {
console.error("[apns-gateway] Transmission error (code %d) for recipient '%s'", errCode, device);
}).on('connected', function (count) {
console.log("[apns-gateway] Connected, %d total sockets", count);
}).on('disconnected', function (count) {
console.log("[apns-gateway] Disconnected, %d total sockets", count);
}).on('transmitted', function (notification, recipient) {
console.log("[apns-gateway] Transmitted '%s' to device '%s'", notification, recipient);
});
//
// APNS FEEDBACK
//
var apnsFeedback = new apn.Feedback(config.apns.feedback.options);
apnsFeedback.on('error', function (err) {
// Emitted when an error occurs initialising the module. Usually caused by failing to load the certificates.
// This is most likely an unrecoverable error.
console.error("[apns-feedback] " + err);
}).on('feedbackError', function (err) {
// Emitted when an error occurs receiving or processing the feedback and in the case of a socket error occurring.
// These errors are usually informational and node-apn will automatically recover.
console.error("[apns-feedback] " + err);
}).on('feedback', function (feedbackData) {
feedbackData.forEach(function (item) {
var time = item.time;
var device = item.device;
// Do something with item.device and item.time;
console.log("[apns-feedback] Should remove device: '%s'", device);
});
});
//
// GCM SENDER
//
var gcmSender = new notify.gcm.Sender(config.gcm.options);
gcmSender.on('error', function (err) {
console.error("[gcm-sender] " + err);
}).on('transmissionError', function (error, registrationId) {
console.error("[gcm-sender] Transmission error (%s) for recipient '%s'", error, registrationId);
}).on('updated', function (result, registrationId) {
console.log("[gcm-sender] Registration ID needs to be updated: '%s'", registrationId);
}).on('transmitted', function (result, registrationId) {
console.log("[gcm-sender] Transmitted '%s' to device '%s'", result, registrationId);
});
function processMessage(message) {
var worker = new RedisLockingWorker({
'client': redisClient,
'lockKey' : config.redis.lock.keyPrefix + message.identifier,
'statusLevel' : RedisLockingWorker.StatusLevels.Verbose,
'lockTimeout' : config.redis.lock.lockTimeout,
'maxAttempts' : config.redis.lock.maxAttempts
});
worker.on("acquired", function (lastAttempt) {
console.log("[redis-client] Acquired lock %s", worker.lockKey);
message.dispatch();
if (config.redis.failoverEnabled)
worker.done(lastAttempt);
else {
console.log("[redis-client] Work complete. Deleting lock %s", worker.lockKey);
worker.done(true);
}
worker = undefined;
});
worker.on("locked", function () {
console.log("[redis-client] Someone else acquired the lock %s", worker.lockKey);
});
worker.on("error", function (error) {
console.error("[redis-client] Error from lock %s: %j", worker.lockKey, error);
});
worker.on("status", function (message) {
console.log("[redis-client] Status message from lock %s: %s", worker.lockKey, message);
});
worker.acquire();
}
messages.APNSMessage.prototype.dispatch = function () {
console.log("[apns-gateway] Sending notification '%s' to device(s) [ '%s' ]",
this.identifier, this.token.join(", "));
// The APNS connection is defined/initialized elsewhere
apnsGateway.send(this);
};
messages.GCMMessage.prototype.dispatch = function () {
console.log("[gcm-sender] Sending notification '%s' to device(s) [ '%s' ]",
this.identifier, this.registrationId.join(", "));
// The APNS connection is defined/initialized elsewhere
try {
gcmSender.send(this);
} catch (err) {
console.error("[gcm-sender] node-gcm error: %s", err);
}
};
if (config.catchExceptions) {
process.on('uncaughtException', function (err) {
console.error('Caught exception: ' + err);
});
}
process.on('exit', function () {
console.log('Dumping redis dataset to disk...');
if (redisClient.connected) {
redisClient.save();
}
console.log('Exiting.');
});