0

I am on the process of learning node express, I wanted to create an app that uses Weather Underground API and notifies the user on extreme weather by SMS or email using send grid or other services. I am not sure about how the server could initiate the notification and am looking for suggestions on what technologies to use to initiate the email or SMS.

tsinat
  • 235
  • 1
  • 3
  • 16

1 Answers1

1

You can achieve with this request npm package, using this package you can create both get and post request. Make a get request to weather api and check whether the weather is critical or not.If it is critical you can emit a message via socket.io to client.

sample code from request npm package documentation.I hope you get the point.you can do something similar.please refer the documentation.

var request = require('request');
request('https://www.wunderground.com/weather/api/', function (error, response, body) {
if (!error && response.statusCode == 200) {
    console.log(response);
    if(response.critical){ 
      io.to(socketid).emit('message', 'message content');
    }
   }
});

you can put this in a setInterval function and call this method every 10 seconds or more and update the client.

you need to store the socket id of the client to push to that particular client.If you want to emit to everyone you can just do socket.emit so everyone receives the notification and to access socket.io inside express routes refer this stack-overflow question

Community
  • 1
  • 1