Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to Integrate WebSockets with React Redux
Next article icon

How to Integrate WebSockets with React Redux

Last Updated : 04 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Integrating WebSockets with Redux allows for real-time bidirectional communication between the client (e.g. a web browser) and the server. This enables applications to push data from the server to the client instantly, facilitating features such as real-time updates, live notifications, and chat applications

Steps to Setup the Backend:

Step 1: Create a new directory for your project and navigate into it in your terminal.

mkdir server
cd server

Step2: Run the following command to initialize a new Node.js project and create a package.json file:

npm init -y

Step 3: Install web socket Dependencies from the given command.

npm install ws

The updated dependencies in package.json file will look like.

"dependencies": {
"ws": "^8.16.0"
}

Example: This example used to setup the backend for the project.

JavaScript
// index.js const WebSocket = require('ws');  const wss = new WebSocket.Server({ port: 8080 });  wss.on('connection', function connection(ws) {     ws.on('message', function incoming(message) {         console.log('Received:', message);         // Echo back the received message         ws.send(message);     }); }); 

Output: Run the server with the following command in the terminal

node index.js

Steps to Setup the Frontend

npx create-react-app foldername

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd foldername

Step 3: Install required dependencies

npm install react-redux redux redux-thunk

Step 4: After setting up react environment on your system, we can start by creating an App.js file and create a directory by the name of components in which we will write our desired function.

Project Structure:

Screenshot-2024-03-22-215520

The updated dependencies in package.json file will look like.

"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^9.1.0",
"react-scripts": "5.0.1",
"redux": "^5.0.1",
"redux-thunk": "^3.1.0",
"web-vitals": "^2.1.4"
},

Approach to integrate WebSocket with React Redux:

  • Configure Redux store with middleware like redux-thunk or redux-saga for handling asynchronous actions, including WebSocket interactions.
  • Create a WebSocket instance for bidirectional real-time communication between the client and server.
  • Create action creators for WebSocket events, such as sending messages to the server and handling incoming messages.
  • Define reducers to update the Redux store based on WebSocket actions, ensuring state predictability and synchronization with server data.
  • Dispatch WebSocket actions from components or middleware to initiate WebSocket communications and reflect real-time updates in the UI through Redux-managed state.

Example: Implementation to showcase the process integrating WebSockets with Redux using chat application.

JavaScript
//src/index.js  import React from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-redux'; import { createStore, applyMiddleware } from 'redux'; import { thunk } from 'redux-thunk'; import rootReducer from './reducers'; import App from './App'; import { connectWebSocket } from './actions/websocketActions';  const store = createStore(   rootReducer,   applyMiddleware(thunk) );  store.dispatch(connectWebSocket());  ReactDOM.render(   <Provider store={store}>     <App />   </Provider>,   document.getElementById('root') ); 
JavaScript
// App.js  import React, { useEffect, useState } from 'react'; import { connect } from 'react-redux'; import {     connectWebSocket,     sendMessage,     receiveMessage } from './actions/websocketActions';  const App = ({ connectWebSocket,     sendMessage, receiveMessage,     messages }) => {     const [messageText, setMessageText] = useState('');      useEffect(() => {         connectWebSocket();     }, [connectWebSocket]);      useEffect(() => {         const messageChannel =             new BroadcastChannel('chat_messages');          messageChannel.onmessage = (event) => {             // Update messages in this tab              // when a message is received from another tab             receiveMessage(event.data);         };          return () => {             messageChannel.close();         };     }, []);      const handleMessageChange = (e) => {         setMessageText(e.target.value);     };      const handleSubmit = (e) => {         e.preventDefault();         if (messageText.trim() !== '') {             sendMessage(messageText);             setMessageText('');         }     };      return (         <div className="App">             <h1>Real-Time Chat Application</h1>             <div className="chat-container">                 {messages.map((message, index) => (                     <div key={index} className="message">                         {message}                     </div>                 ))}             </div>             <form onSubmit={handleSubmit}>                 <input                     type="text"                     value={messageText}                     onChange={handleMessageChange}                     placeholder="Type your message..."/>                 <button type="submit">Send</button>             </form>         </div>     ); };  const mapStateToProps = (state) => ({     messages: state.websocket.messages });  export default connect(mapStateToProps,     {         connectWebSocket,         sendMessage,         receiveMessage     })(App); 
JavaScript
// websocketActions.js  let ws; let messageChannel; let isEventListenerSetup = false;  // Generate a unique identifier for each browser const userId = Math.random().toString(36).substring(7);  export const connectWebSocket = () => (dispatch) => {     // Ensure WebSocket connection is established only once     if (!ws || ws.readyState === WebSocket.CLOSED) {         ws = new WebSocket('ws://localhost:8080');         ws.onopen = () => {             console.log('WebSocket connected successfully!');         };          ws.onmessage = async (event) => {             const message = await event.data.text();             const formattedMessage = `${userId}: ${message}`;              // Broadcast the received message              // to all Broadcast Channel clients             messageChannel.postMessage(formattedMessage);         };          ws.onerror = (error) => {             console.error('WebSocket error:', error);         };          ws.onclose = () => {             console.log('WebSocket connection closed.');         };          // Log the WebSocket object to check          // if it's being created multiple times         console.log('WebSocket:', ws);     }      if (!messageChannel) {         messageChannel = new BroadcastChannel('chat_messages');     }      if (!isEventListenerSetup) {         messageChannel.onmessage = (event) => {         };         isEventListenerSetup = true;     }     dispatch({         type: 'WEBSOCKET_CONNECTED',         payload: ws     }); };  export const sendMessage = (message) => (dispatch) => {     if (ws && ws.readyState === WebSocket.OPEN) {         ws.send(message);     } };  export const receiveMessage = (message) => ({     type: 'WEBSOCKET_MESSAGE_RECEIVED',     payload: message }); 
JavaScript
// WebSocketComponent.js  import React, { useEffect } from 'react'; import { connect } from 'react-redux'; import { connectWebSocket } from '../actions/websocketActions';  const WebSocketComponent = ({ connectWebSocket, messages }) => {     useEffect(() => {         const socket = connectWebSocket();          socket.onopen = () => {             console.log('WebSocket connected successfully!');         };          socket.onerror = (error) => {             console.error('WebSocket error:', error);         };          socket.onclose = () => {             console.log('WebSocket connection closed.');         };          return () => {             socket.close();         };     }, [connectWebSocket]);      return (         <div>             {messages.map((message, index) => {                 console.log('Message:', message);                 return (                     <div                         key={index}                         style={{                             padding: '5px 10px',                             margin: '5px',                             borderRadius: '5px',                             alignSelf: message.source ===                                 'right' ? 'flex-end' : 'flex-start',                             backgroundColor: message.source ===                                 'right' ? '#d3d3d3' : '#f0f0f0',                         }}>                         {message.content}                     </div>                 );             })}         </div>     ); };  const mapStateToProps = (state) => ({     messages: state.websocket.messages, });  export default connect(mapStateToProps,     { connectWebSocket })(WebSocketComponent); 
JavaScript
//src/store/configureStore.js  import { createStore, applyMiddleware } from 'redux'; import { thunk } from 'redux-thunk'; import rootReducer from '../reducers';  const store = createStore(   rootReducer,   applyMiddleware(thunk) );  export default store; 
JavaScript
// reducers/websocketReducer.js  const initialState = {     connection: null,     messages: [] };  const websocketReducer = (state = initialState, action) => {     switch (action.type) {         case 'WEBSOCKET_CONNECTED':             return {                 ...state,                 connection: action.payload             };         case 'WEBSOCKET_MESSAGE_RECEIVED':             return {                 ...state,                 messages: [...state.messages, action.payload]             };         case 'WEBSOCKET_MESSAGE_SENT':             return {                 ...state,                 messages: [...state.messages, `Sent: ${action.payload}`]             };         default:             return state;     } };  export default websocketReducer; 
JavaScript
// src/reducers/index.js  import { combineReducers } from 'redux'; import websocketReducer from './websocketReducer';  export default combineReducers({     websocket: websocketReducer }); 

Steps to run the Application:

npm start

Output: Your project will be shown in the URL http://localhost:3000/

cc


Next Article
How to Integrate WebSockets with React Redux

A

adityaan93uh
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • React-Redux

Similar Reads

    How to Integrate Redux with React Components ?
    Redux is an open-source JavaScript library for managing and centralizing application state. It helps you to write applications that behave consistently and are easy to test and run in different environments. It can also be understood as the predictable state container for the JavaScript app. It is m
    4 min read
    How to use React Context with React-Redux ?
    React context with React-Redux is a popular state management library for React applications. Using React context with React-Redux is a powerful way to provide the Redux store to components deep within your component tree without manually passing it down through props. PrerequisitesNode.js and NPMRea
    3 min read
    How to Send WebSocket Requests with Postman ?
    This article will show how to send WebSocket requests in Postman. Postman is a popular collaborative platform for API development. It offers different tools for designing, debugging, and testing an API, making it more efficient. WebSocket is an advanced technology used for real-time bidirectional co
    3 min read
    Real-time Updates with WebSockets and React Hooks
    The WebSocket protocol provides continuous, real-time, full-duplex communication between a client and server over a single TCP socket connection. The WebSocket protocol has only two plans: opening a handshake and facilitating data transfer. After the server receives the handshake request sent by the
    5 min read
    How to Test WebSocket APIs With Postman?
    WebSocket is a communication protocol that provides full-duplex communication channels over a single, long-lived connection between clients and servers. Unlike HTTP, which is a request-response protocol, WebSocket allows both the client and server to send messages to each other independently at any
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences