zhaopinxinle.com

Real-Time Cryptocurrency Price Tracker Using React and Spring Boot

Written on

Chapter 1: Introduction to the Crypto Price Consumer App

In this guide, we will walk through the creation of a React application named React Crypto Price Consumer. This application will connect to the Crypto Price Producer WebSocket and display real-time cryptocurrency price updates.

For those interested, the complete code and implementation details can be found in the linked article below. Let’s dive in!

Prerequisites

To follow along, ensure that you have npm installed on your machine.

Creating the Crypto Price Consumer React Application

Open your terminal and navigate to the websocket-crypto-simulator folder, then execute the following command:

npx create-react-app react-crypto-price-consumer

This command creates a new React application titled react-crypto-price-consumer. After the setup completes, launch the application in your favorite IDE.

Installing Necessary npm Packages

Next, we need to install several npm packages: antd, sockjs-client, net, and stompjs. In the terminal, navigate to the react-crypto-price-consumer directory and run:

npm install antd sockjs-client net stompjs

Organizing the Codebase

To maintain an organized structure, create a folder named components within the src directory.

Creating the Home.js Component

Inside the components folder, create a file called Home.js with the following code:

import React, { useEffect, useState } from 'react';

import SockJS from 'sockjs-client';

import Stomp from 'stompjs';

import { Layout, Col, Row, Table } from 'antd';

const { Header, Content } = Layout;

function Home() {

const [cryptoUpdates, setCryptoUpdates] = useState([]);

const socket = new SockJS("http://localhost:8080/ws");

const stompClient = Stomp.over(socket);

useEffect(() => {

stompClient.connect({}, connectCallback, errorCallback);

}, []);

const errorCallback = (error) => {

console.error("WebSocket connection error: ", error);

};

const connectCallback = () => {

console.log("Connected");

stompClient.subscribe("/topic/prices", onMessageCallback);

};

const onMessageCallback = (message) => {

const cryptoUpdate = JSON.parse(message.body);

const data = {

key: new Date().getTime(),

datetime: cryptoUpdate.datetime,

crypto: cryptoUpdate.crypto,

price: cryptoUpdate.price,

};

setCryptoUpdates(prevUpdates => [data, ...prevUpdates]);

};

const columns = [

{ title: 'Date/Time', dataIndex: 'datetime', key: 'datetime' },

{ title: 'Crypto', dataIndex: 'crypto', key: 'crypto' },

{ title: 'Price', dataIndex: 'price', key: 'price' },

];

return (

<Layout>

<Header style={{ textAlign: 'center', color: '#fff', backgroundColor: '#333', fontSize: '2em' }}>

Cryptocurrency Prices

</Header>

<Content>

<Table dataSource={cryptoUpdates} columns={columns} rowKey="key" />

</Content>

</Layout>

);

}

export default Home;

The Home component establishes a WebSocket connection using SockJS and Stomp to display real-time cryptocurrency prices. It subscribes to the /topic/prices channel and updates the UI with the incoming data.

Editing the App.js File

Now, we will modify the App.js file with the following content:

import React from 'react';

import Home from './components/Home.js';

function App() {

return <Home />;

}

export default App;

The App component serves as the main entry point, rendering the Home component, which displays our cryptocurrency price updates.

Demonstration of the Application

To start the Crypto Price Producer app, navigate to the root folder of the crypto-price-producer and run:

./mvnw clean spring-boot:run

In a separate terminal window, navigate to the react-crypto-price-consumer root folder and execute:

npm start

To view the price updates, open your browser and navigate to http://localhost:3000.

Conclusion

In this article, we successfully built the React Crypto Price Consumer application. This consumer connects to the Spring Boot Crypto Price Producer WebSocket, allowing us to track real-time cryptocurrency price updates. We demonstrated how both the Producer and Consumer applications function together.

Additional Resources

For further reading, check out the article on running a Real-time Crypto Price Simulator in Minikube (Kubernetes).

Support and Engagement

If you found this article helpful, consider supporting me by engaging with my content:

πŸ‘ Clap, highlight, and respond to my story.

🌐 Share this story on your social media.

πŸ”” Follow me on Medium, LinkedIn, and Twitter.

βœ‰οΈ Subscribe to my newsletter for more updates.

Chapter 2: Building a Crypto Price Tracker App

In this chapter, we will explore the process of building a crypto price tracker app using React Native.

This video guides you through creating a Crypto Price Tracker App with React Native, using APIs, Axios, and Charts.

Continuing with the Crypto Price App

Next, we will delve into a beginner-friendly project that focuses on building a crypto price app using React.

This video serves as a beginner's project for building a crypto price app in React, perfect for those just starting with React development.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Overcoming Shyness: My Path to Confidence and Self-Discovery

A personal journey detailing the struggles with shyness and the steps taken to build self-confidence.

Transforming Fear: From a Curse to a Companion

Discover how to turn fear into a motivating companion rather than a debilitating curse through personal experiences and insights.

Understanding iOS Application States and Lifecycle Transitions

Explore the various states of iOS applications and their transitions, including background rules and lifecycle methods.

Unlock Your Potential: The Art of Strategic Collaboration

Discover how asking the right questions and investing in people can transform your vision into reality.

Metaphysics of Vision: Understanding the Eye's Intricacies

Explore the fascinating processes behind how we see, from light entry to brain interpretation, highlighting the marvels of ocular function.

# Memberships That Offer Great Value and Pay for Themselves

Discover various memberships that can save you money and enhance your lifestyle.

# The Disappearance of Astrologers in a Pandemic World

Exploring the silence of astrologers during the pandemic and the shift towards science amidst crisis.

# Transforming Chaos into Calm: A Simple Question's Power

Discover how a simple question can help you regain control in chaotic situations, promoting clarity and calmness.