# WebSocket or Socket io in react

<div class="WaaZC" id="bkmrk-the-choice-between-u" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div class="rPeykc" data-hveid="CAMQAQ" data-ved="2ahUKEwiZ1OnA5aqRAxUo4zgGHQBsGE4Qo_EKegQIAxAB" style="margin: 0px 0px 10px;"><span data-huuid="2996336202520041438">The choice between using raw WebSockets or [Socket.IO](https://socket.io/) in a React application depends on the specific requirements of the project.</span></div></div></div><div class="WaaZC" id="bkmrk-websockets%3A" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CAUQAQ" data-ved="2ahUKEwiZ1OnA5aqRAxUo4zgGHQBsGE4Qo_EKegQIBRAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="2996336202520042992">WebSockets:</span></div></div></div><div class="WaaZC" id="bkmrk-pros%3A-provides-a-dir" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);">- <div class="zMgcWd dSKvsb" data-il="" style="padding-bottom: 0px; padding-top: 0px; border-bottom: none;"><div data-crb-p=""><div class="xFTqob" style="flex: 1 1 0%; min-width: 0px;"><div class="Gur8Ad" style="font-size: 16px; font-weight: 500; line-height: 24px; overflow: hidden; padding-bottom: 4px; transition: transform 200ms cubic-bezier(0.2, 0, 0, 1); display: inline;"><span data-huuid="2996336202520041227">**Pros:**</span></div><div class="vM0jzc" style="color: rgb(10, 10, 10); display: inline;">
    - <span data-huuid="2996336202520039462">Provides a direct, low-level protocol for real-time communication, offering maximum control and flexibility.</span>
    - <span data-huuid="2996336202520041016">Minimal overhead, leading to potentially higher performance in certain scenarios.</span>
    - <span data-huuid="2996336202520042570">Broad interoperability with various WebSocket servers.</span>
    
    </div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il="" style="padding-bottom: 0px; padding-top: 8px; border-bottom: none;"><div data-crb-p=""><div class="xFTqob" style="flex: 1 1 0%; min-width: 0px;"><div class="Gur8Ad" style="font-size: 16px; font-weight: 500; line-height: 24px; overflow: hidden; padding-bottom: 4px; transition: transform 200ms cubic-bezier(0.2, 0, 0, 1); display: inline;"><span data-huuid="2996336202520040028">**Cons:**</span></div><div class="vM0jzc" style="color: rgb(10, 10, 10); display: inline;">
    - <span data-huuid="2996336202520042359">Requires more boilerplate code for features like reconnection, error handling, and message buffering.</span>
    - <span data-huuid="2996336202520039817">Steeper learning curve for managing connection states and potential issues.</span>
    
    </div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il="" style="padding-bottom: 0px; padding-top: 8px; border-bottom: none;"><div data-crb-p=""><div class="xFTqob" style="flex: 1 1 0%; min-width: 0px;"><div class="Gur8Ad" style="font-size: 16px; font-weight: 500; line-height: 24px; overflow: hidden; padding-bottom: 4px; transition: transform 200ms cubic-bezier(0.2, 0, 0, 1); display: inline;"><span data-huuid="2996336202520041371">**Use Cases:** </span></div><div class="vM0jzc" style="color: rgb(10, 10, 10); display: inline;"><span data-huuid="2996336202520042148">Simple, high-performance applications where fine-grained control over the connection is crucial, such as game data updates or stock tickers.</span></div></div></div></div>

</div></div><div class="WaaZC" id="bkmrk-socket.io%3A" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CB8QAQ" data-ved="2ahUKEwiZ1OnA5aqRAxUo4zgGHQBsGE4Qo_EKegQIHxAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="17132968970867578895">[Socket.IO](https://socket.io/):</span></div></div></div><div class="WaaZC" id="bkmrk-pros%3A-built-on-top-o" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);">- <div class="zMgcWd dSKvsb" data-il="" style="padding-bottom: 0px; padding-top: 0px; border-bottom: none;"><div data-crb-p=""><div class="xFTqob" style="flex: 1 1 0%; min-width: 0px;"><div class="Gur8Ad" style="font-size: 16px; font-weight: 500; line-height: 24px; overflow: hidden; padding-bottom: 4px; transition: transform 200ms cubic-bezier(0.2, 0, 0, 1); display: inline;"><span data-huuid="17132968970867577620">**Pros:**</span></div><div class="vM0jzc" style="color: rgb(10, 10, 10); display: inline;">
    - <span data-huuid="17132968970867580441">Built on top of WebSockets, providing a higher-level abstraction with built-in features like automatic reconnection, event-based communication, and fallbacks (like HTTP long-polling) for environments where WebSockets are not supported.</span>
    - <span data-huuid="17132968970867579591">Simplified development with a feature-rich API and abstractions for common real-time functionalities.</span>
    - <span data-huuid="17132968970867578741">Easier to adopt, especially for developers familiar with Node.js.</span>
    
    </div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il="" style="padding-bottom: 0px; padding-top: 8px; border-bottom: none;"><div data-crb-p=""><div class="xFTqob" style="flex: 1 1 0%; min-width: 0px;"><div class="Gur8Ad" style="font-size: 16px; font-weight: 500; line-height: 24px; overflow: hidden; padding-bottom: 4px; transition: transform 200ms cubic-bezier(0.2, 0, 0, 1); display: inline;"><span data-huuid="17132968970867577891">**Cons:**</span></div><div class="vM0jzc" style="color: rgb(10, 10, 10); display: inline;">
    - <span data-huuid="17132968970867580712">Adds some overhead due to its feature set and metadata, potentially leading to slightly larger message sizes.</span>
    - <span data-huuid="17132968970867579862">Limited interoperability with non-Socket.IO WebSocket servers.</span>
    - <span data-huuid="17132968970867579012">Not designed for global scale in the same way as raw WebSockets.</span>
    
    </div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il="" style="padding-bottom: 0px; padding-top: 8px; border-bottom: none;"><div data-crb-p=""><div class="xFTqob" style="flex: 1 1 0%; min-width: 0px;"><div class="Gur8Ad" style="font-size: 16px; font-weight: 500; line-height: 24px; overflow: hidden; padding-bottom: 4px; transition: transform 200ms cubic-bezier(0.2, 0, 0, 1); display: inline;"><span data-huuid="17132968970867578162">**Use Cases:** </span></div><div class="vM0jzc" style="color: rgb(10, 10, 10); display: inline;"><span data-huuid="17132968970867577737">Applications requiring robust real-time features like chat applications, collaborative editing, real-time analytics, and where ease of development and built-in reliability are priorities.</span></div></div></div></div>

</div></div><div class="WaaZC" id="bkmrk-in-react%3A" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CDkQAQ" data-ved="2ahUKEwiZ1OnA5aqRAxUo4zgGHQBsGE4Qo_EKegQIORAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="13129251272164368110">In React:</span></div></div></div><div class="WaaZC" id="bkmrk-you-can-use-the-nati" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);">- <span data-huuid="13129251272164367385">You can use the native browser WebSocket API directly or leverage libraries like `react-use-websocket` for simplified integration with React's lifecycle and hooks.</span>
- <span data-huuid="13129251272164368267">For [Socket.IO](https://socket.io/), you can use the `socket.io-client` library and manage the connection within your React components, potentially using React Context or custom hooks for global access.</span>

</div></div><div class="WaaZC" id="bkmrk-conclusion%3A" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CEIQAQ" data-ved="2ahUKEwiZ1OnA5aqRAxUo4zgGHQBsGE4Qo_EKegQIQhAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="15426272183334331175">Conclusion:</span></div></div></div><div class="WaaZC" id="bkmrk-choose-websockets-if" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);">- <span data-huuid="15426272183334333852">Choose WebSockets if you need absolute control, minimal overhead, and are comfortable implementing features like reconnection and error handling yourself.</span>
- <span data-huuid="15426272183334332906">Choose [Socket.IO](https://socket.io/) if you prioritize ease of development, automatic handling of common real-time challenges, and built-in features like reconnection and event-based communication.</span>

</div></div><div id="bkmrk-websocket-sample-cod"><div class="WaaZC" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div aria-level="3" class="rPeykc uP58nb rWIipd" data-hveid="CAQQAQ" data-ved="2ahUKEwjo28qF5qqRAxUo4zgGHQBsGE4Qo_EKegQIBBAB" role="heading" style="margin: 0px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="858667837331752369">WebSocket Sample Code in React</span></div></div></div><div class="WaaZC" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div class="rPeykc" data-hveid="CAYQAQ" data-ved="2ahUKEwjo28qF5qqRAxUo4zgGHQBsGE4Qo_EKegQIBhAB" style="margin: 10px 0px;"><span data-huuid="858667837331753975">Using the native WebSocket API in a React component:</span></div></div></div><div class="WaaZC" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">Code</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwjo28qF5qqRAxUo4zgGHQBsGE4Q9MILegQIAhAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div></div>```
import React, { useEffect, useState } from 'react';

function WebSocketComponent() {
  const [messages, setMessages] = useState([]);
  const [inputValue, setInputValue] = useState('');
  const [ws, setWs] = useState(null);

  useEffect(() => {
    const newWs = new WebSocket('ws://localhost:8080'); // Replace with your WebSocket server URL

    newWs.onopen = () => {
      console.log('WebSocket connected');
    };

    newWs.onmessage = (event) => {
      setMessages((prevMessages) => [...prevMessages, event.data]);
    };

    newWs.onclose = () => {
      console.log('WebSocket disconnected');
    };

    newWs.onerror = (error) => {
      console.error('WebSocket error:', error);
    };

    setWs(newWs);

    return () => {
      newWs.close();
    };
  }, []);

  const sendMessage = () => {
    if (ws && ws.readyState === WebSocket.OPEN) {
      ws.send(inputValue);
      setInputValue('');
    }
  };

  return (
    <div>
      <h1>WebSocket Chat</h1>
      <div>
        {messages.map((msg, index) => (
          <p key={index}>{msg}</p>
        ))}
      </div>
      <input
        type="text"
        value={inputValue}
        onChange={(e) => setInputValue(e.target.value)}
      />
      <button onClick={sendMessage}>Send</button>
    </div>
  );
}

export default WebSocketComponent;
```

<div id="bkmrk-socket.io%C2%A0sample-cod"><div class="WaaZC" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div><div class="WaaZC" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div aria-level="3" class="rPeykc uP58nb rWIipd" data-hveid="CA0QAQ" data-ved="2ahUKEwjo28qF5qqRAxUo4zgGHQBsGE4Qo_EKegQIDRAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="8539701963755056614">[Socket.IO](https://socket.io/) Sample Code in React</span></div></div></div><div class="WaaZC" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div class="rPeykc" data-hveid="CA4QAQ" data-ved="2ahUKEwjo28qF5qqRAxUo4zgGHQBsGE4Qo_EKegQIDhAB" style="margin: 10px 0px;"><span data-huuid="8539701963755053808">Using the [Socket.IO](https://socket.io/) client library in a React component:</span></div></div></div><div class="WaaZC" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">Code</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwjo28qF5qqRAxUo4zgGHQBsGE4Q9MILegQICxAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div></div>```
import React, { useEffect, useState } from 'react';
import io from 'socket.io-client'; // Install with: npm install socket.io-client

const socket = io('http://localhost:3001'); // Replace with your Socket.IO server URL

function SocketIOComponent() {
  const [messages, setMessages] = useState([]);
  const [inputValue, setInputValue] = useState('');

  useEffect(() => {
    socket.on('connect', () => {
      console.log('Socket.IO connected');
    });

    socket.on('message', (message) => {
      setMessages((prevMessages) => [...prevMessages, message]);
    });

    socket.on('disconnect', () => {
      console.log('Socket.IO disconnected');
    });

    return () => {
      socket.off('connect');
      socket.off('message');
      socket.off('disconnect');
    };
  }, []);

  const sendMessage = () => {
    socket.emit('message', inputValue); // 'message' is the event name
    setInputValue('');
  };

  return (
    <div>
      <h1>Socket.IO Chat</h1>
      <div>
        {messages.map((msg, index) => (
          <p key={index}>{msg}</p>
        ))}
      </div>
      <input
        type="text"
        value={inputValue}
        onChange={(e) => setInputValue(e.target.value)}
      />
      <button onClick={sendMessage}>Send</button>
    </div>
  );
}

export default SocketIOComponent;
```