File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ .messages {
2+ list-style : none;
3+ }
4+
5+ .message {
6+ margin-bottom : 0.5em ;
7+ }
8+
9+ .chat-box {
10+ position : fixed;
11+ bottom : 0 ;
12+ left : 0 ;
13+ right : 0 ;
14+ padding : 16px ;
15+ display : flex;
16+ height : 7vh ;
17+ gap : 8px ;
18+ }
19+
20+ .chat-input {
21+ flex : 1 ;
22+ border-radius : 8px ;
23+ border : 1px solid # 777 ;
24+ }
25+
26+ .send-button {
27+ background-color : # 12bc00 ;
28+ color : # fff ;
29+ border-radius : 8px ;
30+ border : none;
31+ }
32+
33+ @media screen and (min-width : 800px ) {
34+ .container {
35+ margin : auto 30% ;
36+ }
37+ .chat-box {
38+ height : 5vh ;
39+ left : 25% ;
40+ right : 25% ;
41+ }
42+ }
Original file line number Diff line number Diff line change 1- import { io } from "socket.io-client" ;
1+ import { useState } from "react" ;
2+ import useSocket from "./hooks/useSocket" ;
23import "./App.css" ;
34
45const App = ( ) => {
5- const socket = io ( "http://localhost:3000" ) ;
6+ const { messages, sendMessage } = useSocket ( "http://localhost:3000" ) ;
7+ const [ input , setInput ] = useState ( "" ) ;
68
7- socket . on ( "connect" , ( ) => {
8- console . log ( socket . id ) ;
9- } ) ;
9+ const handleSend = ( ) => {
10+ if ( ! input . trim ( ) ) return ;
11+ sendMessage ( input . trim ( ) ) ;
12+ setInput ( "" ) ;
13+ } ;
1014
11- socket . on ( "message" , ( data ) => console . log ( data ) ) ;
12-
13- return < > </ > ;
15+ return (
16+ < div className = "container" >
17+ < ul className = "messages" >
18+ { messages . map ( ( message , i ) => (
19+ < li className = "message" key = { i } >
20+ { message . text }
21+ </ li >
22+ ) ) }
23+ </ ul >
24+ < div className = "chat-box" >
25+ < input
26+ className = "chat-input"
27+ value = { input }
28+ onChange = { ( e ) => setInput ( e . target . value ) }
29+ />
30+ < button className = "send-button" onClick = { handleSend } >
31+ Send
32+ </ button >
33+ </ div >
34+ </ div >
35+ ) ;
1436} ;
1537
1638export default App ;
Original file line number Diff line number Diff line change 1+ import { useEffect , useRef , useState } from "react" ;
2+ import { io } from "socket.io-client" ;
3+
4+ const useSocket = ( url ) => {
5+ const socketRef = useRef ( null ) ;
6+ const [ messages , setMessages ] = useState ( [ ] ) ;
7+
8+ useEffect ( ( ) => {
9+ socketRef . current = io ( url ) ;
10+
11+ socketRef . current . on ( "history" , ( history ) => {
12+ setMessages ( history . map ( ( message ) => ( { ...message } ) ) ) ;
13+ } ) ;
14+
15+ socketRef . current . on ( "message" , ( message ) => {
16+ setMessages ( ( prev ) => [ ...prev , message ] ) ;
17+ } ) ;
18+
19+ return ( ) => socketRef . current . disconnect ( ) ;
20+ } , [ url ] ) ;
21+
22+ const sendMessage = ( text ) => {
23+ const message = {
24+ text,
25+ time : new Date ( ) . toISOString ( ) ,
26+ } ;
27+ socketRef . current . emit ( "message" , message ) ;
28+ setMessages ( ( prev ) => [ ...prev , { ...message } ] ) ;
29+ } ;
30+
31+ return { messages, sendMessage } ;
32+ } ;
33+
34+ export default useSocket ;
Original file line number Diff line number Diff line change 1+ [
2+ {
3+ "text" : " Hello" ,
4+ "time" : " 2026-05-22T14:53:00.281Z"
5+ },
6+ {
7+ "text" : " How are you?" ,
8+ "time" : " 2026-05-22T14:54:49.729Z"
9+ },
10+ {
11+ "text" : " I'm good. How are you?" ,
12+ "time" : " 2026-05-22T14:55:22.839Z"
13+ },
14+ {
15+ "text" : " I'm not too bad" ,
16+ "time" : " 2026-05-22T14:55:54.424Z"
17+ }
18+ ]
Original file line number Diff line number Diff line change 11import express from 'express' ;
22import { createServer } from 'http' ;
33import { Server } from 'socket.io' ;
4+ import chat from './data/chat.json' with { type : 'json' } ;
45
56const app = express ( ) ;
67const server = createServer ( app ) ;
@@ -11,14 +12,11 @@ const io = new Server(server, {
1112} ) ;
1213
1314io . on ( 'connection' , ( socket ) => {
14- console . log ( `Client connected: ${ socket . id } ` ) ;
15+ socket . emit ( 'history' , chat ) ;
1516
16- socket . on ( 'message' , ( { from, message } ) => {
17- socket . broadcast . emit ( 'message' , { from, message } ) ;
18- } ) ;
19-
20- socket . on ( 'disconnect' , ( ) => {
21- console . log ( `Client disconnected: ${ socket . id } ` ) ;
17+ socket . on ( 'message' , ( msg ) => {
18+ chat . push ( msg ) ;
19+ socket . broadcast . emit ( 'message' , msg ) ;
2220 } ) ;
2321} ) ;
2422
You can’t perform that action at this time.
0 commit comments