How to transfer data from a controller to a socket io in nodejs

0 votes

I'm developing a web application that will use React to display data on the client side after receiving it from an API using Node.js as the backend. I've never used socket.io, though.

Sockets.ts

function socket( io ){
    io.on("connection", socket => {
        var socketId = socket.id;
        var clientIp = socket.request.connection.remoteAddress;
        console.log('New connection ' + socketId + ' from ' + clientIp);
        
        socket.on("disconnect", () => {
          console.log("Client disconnected");
        });
      
      });
}

export default socket;

router.controller.ts

export const getData: RequestHandler = async (req, res) => {

  
  const options= {
    method: 'GET',
  };

  const response = await fetch(citybikeurl, options)
    .then((res: any) => res.json())  
    .catch((e: any) => {
      console.error({ error: e });
    });
  
  
  console.log("RESPONSE: ", response);
  res.json(response);
  
}

routes.ts

router.get('/', dataController.getData)

At the moment, I don't know if I'm passing any data from controller.ts to Sockets.ts, and after of this be able to emit the results.

Jun 23, 2022 in Node-js by Vaani
• 7,070 points
2,674 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Node-js

0 votes
1 answer

How to disconnect from tcp socket in NodeJs?

Hello, Try this out: net.createConnection() returns a Socket object. client.destroy() is what ...READ MORE

answered Nov 30, 2020 in Node-js by Niroj
• 82,840 points
4,101 views
0 votes
1 answer

How to use executables from a package installed locally in node_modules?

Hello @kartik, Use the npm bin command to get the ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,840 points
1,549 views
0 votes
1 answer

How to extract request http headers from a request using NodeJS connect?

Hello @kartik, To see a list of HTTP ...READ MORE

answered Jul 15, 2020 in Node-js by Niroj
• 82,840 points
23,221 views
0 votes
1 answer

How to get path from the request in nodejs?

Hello @kartik, Try this out: var http = require('http'); var ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,840 points
4,516 views
0 votes
1 answer

How to provide a mysql database connection in single file in nodejs?

Hello @kartik, You could create a db wrapper ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,840 points
9,077 views
0 votes
1 answer

How to split and modify a string in NodeJS?

Hello @kartik, Use split and map function: var str = "123, 124, 234,252"; var ...READ MORE

answered Oct 16, 2020 in Node-js by Niroj
• 82,840 points
1,188 views
0 votes
1 answer

How can i export socket.io into other modules in nodejs?

Because app.js is usually the main initialization ...READ MORE

answered Jun 17, 2022 in Node-js by Neha
• 9,020 points
4,284 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP