Is there any way to update or call api realtime without periodic-api-call method

0 votes
With the help of code, can you tell me if there is any way to update or call the API in real time without the periodic API call method?
Apr 1 in Node-js by Nidhi
• 15,620 points
30 views

1 answer to this question.

0 votes

Yes, you can update or call an API in real-time without periodic polling (e.g., setInterval) by using WebSockets, Server-Sent Events (SSE), or Webhooks. 

1. WebSockets

How it works: Establishes a persistent, bidirectional connection between the client and server. The server can push updates to the client instantly when data changes.

Use case: Real-time apps like chat, live notifications, or stock updates.

Implementation (React example):

import { useEffect } from 'react';

const RealTimeComponent = () => {

  useEffect(() => {

    const socket = new WebSocket('ws://your-api-endpoint');

    socket.onopen = () => console.log('Connected');

    socket.onmessage = (event) => {

      const data = JSON.parse(event.data);

      console.log('Real-time update:', data);

    };

    socket.onclose = () => console.log('Disconnected');

    return () => socket.close(); // Cleanup on unmount

  }, []);

  return <div>Real-time data here</div>;

};

export default RealTimeComponent;

answered Apr 3 by anonymous

Related Questions In Node-js

0 votes
0 answers
0 votes
1 answer

Is there a way to download videos from YouTube Studio using NodeJS

Try this project in the github repository ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
2,094 views
0 votes
1 answer

What is the best way to trigger change or input event in react js?

To handle change or input events in ...READ MORE

answered Feb 22 in Node-js by Kavya
102 views
0 votes
1 answer

How can I implement user authentication with JWT in an Express.js app?

In an Express.js application, you can use ...READ MORE

answered Dec 17, 2024 in Java-Script by Navya
142 views
0 votes
1 answer

Is it possible to handle React events using the Chrome extension?

Yes, it's possible to handle React events ...READ MORE

answered Feb 22 in Node-js by Kavya
70 views
0 votes
1 answer

How can I use all the React events with Material-UI components?

The best approach is to leverage the ...READ MORE

answered Feb 22 in Node-js by Kavya
73 views
0 votes
1 answer

Why won't React events fire, or what could prevent them from firing?

If React events are not firing, several ...READ MORE

answered Feb 22 in Node-js by Kavya
75 views
0 votes
1 answer
0 votes
1 answer
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