简体   繁体   中英

How to make a REST-API url emit data via Socket.IO?

I am new to NodeJS/ExpressJS and Socket.io,my question may sound funny apologies for that. As far as I have gone through the REST-API and Socket.io they have complete different applications.

But what if...

I have a stream API that is streaming data from sensors, now I am saving the data in Database but apart from just saving the data in database only , if my API get a call on specific route then also stream that coming data to the client side also and save it to the database, when the client disconnect the connection then it simply saved to database and when client again makes a connection then the old unseen data is streamed and the stream continues to emit the data at client side.

For eg : let say my rest-api have urls as -

  1. http://:/toWatch

  2. http://:/analyses

  3. http://:/showLiveSensersData

Now the first 2 urls have straight forward approach to send response.

But on the third url, I want to stream the data to client via socket. Now my question is, if the approach is ok and if it can be done then how? Well what I saw was that the Socket.io instance can not be accessed in route file of expressjs.

Any suggestion and solution or help will be much appreciated.

You might be able to pass your instance of io to other your routes through the request object. Something like this:

app.use(function(req,res,next){ req.io = io; next(); });

and then reference it in your routes using req.io. Not sure if there are problems with using it this way, but I have done it before and it has worked with express router.

Even I am struggling with the same functionality as you had faced earlier. I was confused how to fetch the responses from the URL of my sensor and how to return them so that I could use that to display as charts on dashboard. Can you please help me with the sample code? But before that, please do check my sample code here. This is Server.js file. I want to get the dynamic response of link-variable and send that to the emit event.

const express = require('express');
const app = express();
const port = 3000;
var url = require('url');
var fs = require('fs');
const server = require('http').createServer();
var link = "http:www.xample.com/usr/api/data/:deviceId";
server.listen(port,function(){
    console.log("Server Listening on port: "+port);
});

const io = require('socket.io')(server);
io.on('connection',function(socket){
setInterval(function(){
    socket.emit('date', 
    {
        'date': new Date().getDate(),
        'hour': new Date().getHours(),
        'minutes': new Date().getMinutes(),
        'seconds': new Date().getSeconds()
    });           
}, 1000);

socket.emit("welcome","Socket Connection is successful!");
});

const iot = require('socket.io-client');
let socket = iot.connect("http://localhost:3000");
socket.on("date",function(data){
    console.log(data);
});

socket.on("welcome",function(data){
    console.log("Connection Status: ", data);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM