Skip to content

Commit

Permalink
Commit everything
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrognes committed Mar 22, 2023
1 parent e9732c1 commit a076000
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const BaseURL = 'http://localhost:8000';
function App() {
return (
<div className="app">
<h1>Backend listeners</h1>
<ServerSentEvents />
<FetchWithPolling />
</div>
Expand Down
7 changes: 5 additions & 2 deletions src/components/Polling.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Table } from './Table';

export const FetchWithPolling = () => {
const [stockPrices, setStockPrices] = useState([]);
const [numOfConnections, setNumOfConnections] = useState(0);

const fetchStockPrice = () => {
fetch(`${BaseURL}/realtime-price-polling`).then((res) => res.json().then((result) => setStockPrices(result.data)));
Expand All @@ -13,16 +14,18 @@ export const FetchWithPolling = () => {
fetchStockPrice();
const pollingInterval = setInterval(() => {
fetchStockPrice();
setNumOfConnections(numOfConnections + 1)
}, 5000); // poll every 5 seconds

return () => {
clearInterval(pollingInterval);
};
}, []);
}, [numOfConnections]);

return (
<div>
<h1>Polling</h1>
<h2>Polling</h2>
<p>Connections: {numOfConnections}</p>
<Table stockPrices={stockPrices} />
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/ServerSentEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const ServerSentEvents = () => {

return (
<div>
<h1>Server Sent Events</h1>
<h2>Server Sent Events</h2>
<Table stockPrices={stockPrices} />
</div>
);
Expand Down
5 changes: 5 additions & 0 deletions src/components/WebSocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


export const WebSocket = () => {

}

0 comments on commit a076000

Please sign in to comment.