0

I'm trying to add notification in my website. I'm new to node.js and service workers. I’m using expressjs for the backend and a Mysql database.

I'm following this tutorial: Beginners guide to Web Push Notifications using Service Workers and everything worked well until I use a real database for the webpush subscription. Datas are correctly registered in my table but when I try to get those datas with SELECT for testing the send part, I got this error:

C:\...\web-push-lib.js:87
      throw new Error('You must pass in a subscription with at least '
            ^

Error: You must pass in a subscription with at least an endpoint.

I know that the problem comes from the following part but I can’t find out how to use the function to get the result of the query.

//route to test send notification
app.get('/send-notification', (req, res) => {
    //connect to the pool
    pool.getConnection(function (err, connection) {
        if (err) throw err; // not connected!
        const sub = { subscription: null };
        connection.query(`SELECT webpushsub_info FROM webpushsub`, function (err, result, fields) {
          // if any error while executing above query, throw error
          if (err) throw err;
          // if there is no error, you have the result
          sub.subscription = result;
          const message = 'Inscription done';
          webpush.sendNotification(sub.subscription, message)
          connection.release();
          res.json({ message: 'message sent' });
    });
  });
})

EDIT

When I log my result It returns this.

RowDataPacket {
   webpushsub_info: '{ //that's the column name where I store subscriptions
      "endpoint":"https://fcm.googleapis.com/fcm/send/some_endpoint",
      "expirationTime":null,
      "keys":{
          "p256dh":"some key",
          "auth":"an other key"
       }
   }',
...
}

I also tried to log only one element result[0].webpushsub_info so I got exactly the same as the entry. "The expected format is the same output as JSON.stringify'ing a PushSubscription in the browser." -npm documentation for web-push.

Moha
  • 21
  • 3
  • welcome to stackoverflow moha! can you share the link to the tutorial? also, have you checked what lies on line 87 of `web-push-lib.js`? is it your code or a package you have installed? – Bagus Tesa Nov 18 '22 at 09:11
  • though, i saw `sub.subscription = result;` - can you check that `result` is not a null or an empty list? – Bagus Tesa Nov 18 '22 at 09:12
  • flying blind with just the error message i found [this QA](https://stackoverflow.com/questions/57495393/error-when-sending-web-push-notification-no-endpoint). – Bagus Tesa Nov 18 '22 at 09:19
  • First of all, thank you for your quick answer, I'm impressed! About the `web-push-lib.js`, it is from [web-push](https://www.npmjs.com/package/web-push) package I've installed with npm, the `.sendNotification(pushSubscription, payload, options)` method wait for something like I've stored in my database as `pushSubscript` parameter. I'll take a look on the QA you linked, thank you again ! – Moha Nov 18 '22 at 10:26

1 Answers1

0

JSON.parse was the key

After a good week-end of reflexion, I found the solution. Even if

The expected format is the same output as JSON.stringify'ing a PushSubscription in the browser. -npm documentation for web-push.

I had to JSON.parse the result to get an object/array and iterate on every results to send notification to all subscribed users.

app.get('/send-notification', (req, res) => {
  //connect to the pool
  pool.getConnection(function (err, connection) {
    if (err) throw err; // not connected!
    connection.query(`SELECT webpushsub_info FROM webpushsub`, function (err, results, fields) {
      // if any error while executing above query, throw error
      if (err) throw err;
      // if there is no error, you have the result
      results.forEach(result => {
         var pushSubscription = JSON.parse(result.webpushsub_info);
         var message = "Post envoyé";
         console.log(pushSubscription);
         webpush.sendNotification(pushSubscription, message).catch(err=>console.error(err));
      });
      connection.release();
    });
  });
});
Moha
  • 21
  • 3