I am trying to receive push messages from GCM. I simply followed the tutorial and registered the service worker as below:-
function urlB64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
var subscribeForPush = function() {
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey : urlB64ToUint8Array('<API-KEY>')
})
.then(function(subscription) {
console.log("Subscription for Push successful: ", subscription);
})
.catch(function(error) {
console.log("Subscription for Push failed", error);
});
});
};
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
subscribeForPush();
}).catch(function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}
Now, I get the device Id ("cw4jK8NhJgY:APA91bHr64E_kSdh7kN_VjcZRKulPf8KPLJLBjtnHI2qkYzx3-I9aUhunjzVcJjLtkHl9zvN8ys80gADK8tV8SueLX1R2jS0xgrf1Ur6cDw3jNjloUJp8PtWaIN-cEKXj69TZ9-D2Hiw")
from url:- "https://android.googleapis.com/gcm/send/cw4jK8NhJgY:APA91bHr64E_kSdh7kN_VjcZRKulPf8KPLJLBjtnHI2qkYzx3-I9aUhunjzVcJjLtkHl9zvN8ys80gADK8tV8SueLX1R2jS0xgrf1Ur6cDw3jNjloUJp8PtWaIN-cEKXj69TZ9-D2Hiw"
In my service worker file, I simply listen to push messages like:-
self.addEventListener('push', function(event) {
console.log(event)
});
I am able to send messages to gcm using terminal and get the message:-
{"multicast_id":4983340130324592129,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1483043226210433%cc9b4facf9fd7ecd"}]}
But I dont know why I dont see logs in push listener? I am stuck in this issue for more than 2 days. Can anyone please help me here? I am doing this in localhost.
You are not returning the pushManager subscribe
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
return serviceWorkerRegistration.pushManager.subscribe({
'userVisibleOnly': true
}).then(function(subscription) {
if (!subscription) {
// Set appropriate app states.
return;
}
console.log(subscription.endpoint);
}).catch(function (err) {
console.log('error in subcription .. '+ err);
});
})
Check my github project https://github.com/bvakiti/bvakiti.github.io/blob/master/app.js
Let me know if you need any details
I would like to add to bvakiti's solution by declaring
applicationServerKey: convertedVapidKey
in the pushManager subscribe. Most of the tutorials I found in the internet just uses bvakiti's method and most of the devs who follow that tutorial encountered issues in receiving the push notifications.
I was facing the same issue. I updated my Google Chrome version and it fixed the issue.
If you are sure your service worker is registered in the browser but your 'push' event is not getting triggered, you can debug all incoming push notifications to Google Chrome by doing the following:
Open chrome://gcm-internals
Start recording
Send a test push notification to the browser
Observe the logs
The server was getting a 201 on the push request and the browser threw no errors in the console log. However, I was having a decryption issue. The browser was receiving the push notification but failed to decrypt it: AES-GCM decryption failed.
Helpful Source: https://developers.google.com/web/fundamentals/push-notifications/common-issues-and-reporting-bugs
Related
Is it possible to perform a lookup of a Bluetooth device given its address in a Nodejs script?
There are a few packages out there, the main one being Noble. However, they all focus around scanning, and not looking up a known address (as far as i can tell anyway!).
What i want to achieve, is to look up a known address, to see if the device can be found.
Much like PyBluez does for Python:
bluetooth.lookup_name('CC:20:E8:8F:3A:1D', timeout=5)
In Python, this can find the device even if it is undiscoverable, unlike a typical inquiry scan would.
I had this same problem and just found the btwatch lib, but it isn't working for me on the latest raspbian. But the source is just calling l2ping and looking for a string that I'm guessing no longer prints on success, so the modified code below works instead, similar to the lookup_name method, once you have l2ping installed (I think npm bluetooth or pybluez has it)
var Spawn = require('child_process').spawn;
function detectMacAddress(macAddress, callback)
{
//var macAddress = '72:44:56:05:79:A0';
var ls = Spawn('l2ping', ['-c', '1', '-t', '5', macAddress]);
ls.stdout.on('data', function (data) {
console.log("Found device in Range! " + macAddress);
callback(true);
});
ls.on('close', function () {
console.log("Could not find: " + macAddress);
callback(false);
});
}
Or, a synchronous way,
var execSync = require('child_process').execSync;
function detectMacAddressSync(macAddress)
{
var cmd = 'l2ping -c 1 -t 5 ' + macAddress;
try
{
var output = execSync(cmd );
console.log("output : "+ output );
return true;
}
catch(e)
{
console.log("caught: " + e);
return false;
}
}
As far as I have understood the problem you want to connect to the device using address. Then, I would suggest using node-bluetooth-serial-port.
var btSerial = new (require('bluetooth-serialport')).BluetoothSerialPort();
btSerial.on('found', function(address, name) {
btSerial.findSerialPortChannel(address, function(channel) {
btSerial.connect(address, channel, function() {
console.log('connected');
btSerial.write(new Buffer('my data', 'utf-8'), function(err, bytesWritten) {
if (err) console.log(err);
});
btSerial.on('data', function(buffer) {
console.log(buffer.toString('utf-8'));
});
}, function () {
console.log('cannot connect');
});
// close the connection when you're ready
btSerial.close();
}, function() {
console.log('found nothing');
});
});
BluetoothSerialPort.findSerialPortChannel(address, callback[, errorCallback])
Checks if a device has a serial port service running and if it is found it passes the channel id to use for the RFCOMM connection.
callback(channel) - called when finished looking for a serial port on the device.
errorCallback - called the search finished but no serial port channel was found on the device. Connects to a remote bluetooth device.
bluetoothAddress - the address of the remote Bluetooth device.
channel - the channel to connect to.
[successCallback] - called when a connection has been established.
[errorCallback(err)] - called when the connection attempt results in an error. The parameter is an Error object.
I have started learning Node JS today and have made a chatroom.
When a user connects, it sends their user information to Node and that will store that in a var called user which is created in the anonymous function of io.on('connecting', function ... so I can have multiples of them for multiple users (I think that would work)
When the user disconnects, I remove an object of their user from usersOnline which is indexed by their user.id
I keep getting this error that tells me it is undefined:
Error TypeError: Cannot read property 'id' of undefined
at Socket.<anonymous> (/var/www/html/projects/hbc_chat/index.js:28:27)
The error is where I use delete usersOnline[user.id]
And here is the code:
// server user
var srvUser = {
id: 0,
display_name: 'HabboCreate',
fancy_display_name: 'HabboCreate',
picture: 'http://www.habbocreate.com/userdata/uploads/2f48a19f199bb5f018b3089fd4967902'
};
var usersOnline = {};
io.on('connection', function(socket) {
var user;
socket.on('connected', function(userObj) {
// store user
user = userObj;
// add to users online list
usersOnline[user.id] = user;
// send to clients
updateUsersOnline();
// send joined message
sendMessage(srvUser, user.display_name + ' joined the chat');
console.log(user.display_name + ' connected');
});
socket.on('disconnect', function() {
try {
// remove their user from the online list
delete usersOnline[user.id];
// send to client
updateUsersOnline();
// send left message
sendMessage(srvUser, user.display_name + ' left the chat');
}
catch (err) {
console.log('Error', err);
}
});
....
This does not seem to be a scope issue. Is it possible updateUsersOnline() changes the user info? I just ran this exact code, very similar to yours, without errors:
var usersOnline = {};
io.on('connection', function (socket) {
var user;
socket.on('connected', function (userObject) {
user = userObject;
console.log('user: ' + JSON.stringify(user));
usersOnline[user.id] = user;
console.log(JSON.stringify(usersOnline) + '\n');
});
socket.on('disconnect', function () {
try {
console.log('user: ' + JSON.stringify(user));
delete usersOnline[user.id];
console.log(JSON.stringify(usersOnline) + '\n');
} catch (err) {
console.log('err: ' + err);
}
});
});
I passed a user created like this in the client 'connected' event:
var userObject = {
id: new Date(),
display_name: 'some_name',
};
my_connection.emit('connected', userObject);
After the connected event I closed the connection. This is what the server logged:
user: {"id":"2016-12-17T04:49:05.123Z","display_name":"some_name"}
{"2016-12-17T04:49:05.123Z":{"id":"2016-12-17T04:49:05.123Z","display_name":"some_name"}}
user: {"id":"2016-12-17T04:49:05.123Z","display_name":"some_name"}
{}
No errors, so it seems something is removing the user object or at least its id property in between your connected and disconnect events.
right, well I've figured out what was wrong.
It was where I was on the chatroom page, I would restart the node server, my JavaScript wouldnt emit the user details (It only done so upon load), so when i went to refresh or whatever, disconnect event wouldn't be able to find info in the user variable and error
My fix was to make a request user event client side and then emit that on the server when there's a new connection, as to not rely on the page load sending the details
I am trying to implement a feature where I can press on a button to delete some properties of my model.
My Model is:
Request:
status
shopper
I am using an event based structure:
This is the front end
a.requests-deny(href='#', data-id=request.id) Deny request
and this is my backend
var Request = require('../../models/Request');
var ShopperRequests = Backbone.View.extend({
el: '.shoppers-requests',
events: {
'click .requests-deny' : 'requestDeny'
},
requestDeny: function(e){
e.preventDefault();
var target = $(e.currentTarget);
var requestId = target.data('id');
console.log(requestId);
Request.findById(requestId, function(err, request) {
console.log(request);
if (err) {
return console.log('oh no! error', err);
} else {
request.status = 'pending';
request.shopper = '';
request.save(function(err) { // <-- save it back to the database
if (err) {
console.log('oh no! could not be saved in db', err);
} else {
console.log(request);
}
});
}
});
},
Looking at the console in the browser I see that first of all I can't use "require" because "it's not defined"
Uncaught ReferenceError: require is not defined(anonymous function) # shopper-f28ac0daf1d6a206b3172e4b9c670dd4.js:1
I was thinking of taking the requestId and just updating the values in the database but apparently this is not working.
Any idea if this could work or how else I could implement it?
You are not loading the RequireJs library in your backend. Thats why you are getting require is not defined.
If you are using Node, add it like this:
npm install requirejs
Or else, download it from here, and add it to your project with a script tag.
I am writing code to create an online c++ compiler in node.js environment.Using spawn function i created a child process which will compile the code and execute it and send the output back to the user.
But i need to send input to the running c++ program . I used child.stdin.write('data');
for sending data to child but i think cin in the program is not receiving the input .
Please help me to send the input to the running c++ code.
Thanks.
1.Create a file with input data.
2.Create a ReadStream opening input file.
3.Pipe the ReadStream with childprocess.stdin.
file=fs.createReadStream('input.txt',{encoding:'utf8'});
file.pipe(childprocess.stdin);
This worked for me .
You should probably use either cluster, or fork if you want to pass messages... if you do this, node will setup IPC for you to be able to communicate via process.send
Alternatively, you could use a pub/sub system for communication channels (Redis works well for this), this is also your best bet if you need communications across servers.
Below is an example of an older work script...
var env = process.env.NODE_ENV || 'dev';
var cluster = require("cluster");
//TODO: need to adjust to use domains for this work
process.on('uncaughtException', function (err) {
console.error('GENERAL EXCEPTION IN %s: %s', process.env.WORKER_TYPE || 'MASTER',err);
if (err.stack) console.error(err.stack);
if (cluster.isWorker) {
//process.send notifies the parent process of the error
process.send({
err: {
"str": err && err.toString() || "unknown error"
,"message": err && err.message || null
,"stack": err && err.stack || null
}
});
}
process.nextTick(function(){
process.exit(666);
});
});
if (cluster.isMaster) startMaster();
if (cluster.isWorker) startWorker();
function startMaster() {
createWorker("foo");
createWorker("bar");
}
function createWorker(workerType) {
var worker = cluster.fork({"WORKER_TYPE":workerType}); //passes environment variables to child
worker.on('online',onOnline.bind(null, worker));
worker.on('message',onMessage.bind(null, worker));
worker.on('exit',onExit.bind(null, worker));
worker.workerType = workerType;
return worker;
// you can use worker.send() to send a message that
// will raise a message event in the child
}
function startWorker() {
console.log("Running Worker: %s %s", cluster.worker.id, process.env.WORKER_TYPE);
setTimeout(process.exit.bind(process,0), 5000); //close in 5 seconds
//you may want to load a specific module based on WORKER_TYPE
}
function onOnline(worker) {
console.log("Worker Online: %s %s", worker.id, worker.workerType);
//console.log(arguments);
}
function onMessage(worker, msg) {
if (msg.err) {
console.warn("Error From", worker.id, worker.workerType, msg.err);
} else {
console.log("Message From", worker.id, worker.workerType);
}
//console.log(arguments);
}
function onExit(worker, code, signal) {
console.log("Worker Exited: %s %s %s %s", worker.id, worker.workerType, code, signal);
if (env == 'dev') {
//for now just exit the whole thing (dev mode)
process.nextTick(function(){
process.exit(1);
});
} else {
//workers should simply keep working...
//fire off a new worker
createWorker(worker.workerType);
}
}
How can I fail a PULL to get retransmit from the pusher?
In the following example, if I uncomment the // throw ... to trigger an exception on every received message, all the PULL messages are failing, but when I put back the comment, all those messages are lost and not retransmitted by the pusher.
How can I make sure the pusher will retransmit the messages if the PULL method fail?
P.S. The following code is JavaScript in Meteor framework on top of nodejs, but it's not relevant to the problem as I suppose it's similar in every zmq implementations.
var zmq = Npm.require(modulePath + '/zmq');
var sock = zmq.socket('pull');
sock.identity = 'receiving' + process.pid;
sock.bind('tcp://172.17.4.25:5551', function(err) {
if (err) throw err
if (debug) console.log('Listening for ZMQ messages')
});
sock.on('message', Meteor.bindEnvironment(function(data) {
// throw { name: 'MessagePullFailed', message: 'Failing Pull message to check if ZMQ can recover from it' }
var jsondata = JSON.parse(data)
if (jsondata.cmd == 'upsert') {
jsondata['set']['utctime'] = new Date(jsondata['set']['utctime'])
Mon.upsert({_id: jsondata._id}, {'$set': jsondata['set']}, function(err) {
if (err) console.log(err)
})
}
}))