-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add websockets example to nextjs playground (#1605)
- Loading branch information
1 parent
1fc5f2c
commit 6f9e089
Showing
6 changed files
with
291 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Server } from 'socket.io' | ||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
import type { Server as HttpServer } from 'http' | ||
import type { Socket as NetSocket } from 'net' | ||
|
||
// Extend the response object to include the custom `io` property | ||
interface CustomServer extends HttpServer { | ||
io?: Server | ||
} | ||
|
||
interface CustomNetSocket extends NetSocket { | ||
server: CustomServer | ||
} | ||
|
||
interface CustomNextApiResponse extends NextApiResponse { | ||
socket: CustomNetSocket | ||
} | ||
|
||
export default function handler(req: NextApiRequest, res: CustomNextApiResponse) { | ||
if (!res.socket.server.io) { | ||
const io = new Server(res.socket.server) | ||
res.socket.server.io = io | ||
|
||
io.on('connection', (socket) => { | ||
// eslint-disable-next-line no-console | ||
console.log('User connected', socket.id) | ||
|
||
socket.on('send chat message', (msg) => { | ||
socket.emit('message', msg) | ||
}) | ||
|
||
socket.on('disconnect', () => { | ||
// eslint-disable-next-line no-console | ||
console.log('User disconnected', socket.id) | ||
}) | ||
}) | ||
} | ||
res.end() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useState, useEffect } from 'react' | ||
import io, { Socket } from 'socket.io-client' | ||
import { DefaultEventsMap } from 'socket.io' | ||
|
||
const Chat = () => { | ||
// State to store the messages | ||
const [messages, setMessages] = useState<any[]>([]) | ||
// State to store the current message | ||
const [currentMessage, setCurrentMessage] = useState('') | ||
const [socket, setSocket] = useState<Socket<DefaultEventsMap, DefaultEventsMap> | null>(null) | ||
|
||
useEffect(() => { | ||
// Create a socket connection | ||
const createdSocket = io() | ||
|
||
// Listen for incoming messages | ||
createdSocket.on('message', (message) => { | ||
setMessages((prevMessages) => [...prevMessages, message]) | ||
}) | ||
|
||
setSocket(createdSocket) | ||
|
||
// Clean up the socket connection on unmount | ||
return () => { | ||
createdSocket.disconnect() | ||
} | ||
}, []) | ||
|
||
const sendMessage = () => { | ||
if (!socket) { | ||
// eslint-disable-next-line no-console | ||
console.log('socket not running, ruh roh!') | ||
return | ||
} | ||
|
||
socket.emit('send chat message', currentMessage) | ||
setCurrentMessage('') | ||
} | ||
|
||
return ( | ||
<div className={'w-full min-h-96 flex-col space-y-2'}> | ||
<div className="flex flex-row justify-between items-center border border-gray-300 rounded p-2 space-x-2"> | ||
<input | ||
className={'flex-1 border rounded px-2 py-1'} | ||
type="text" | ||
value={currentMessage} | ||
onChange={(e) => setCurrentMessage(e.target.value)} | ||
/> | ||
|
||
<button onClick={sendMessage}>Send</button> | ||
</div> | ||
|
||
<div className="flex flex-col border rounded px-2 py-1"> | ||
{messages?.length === 0 && <p>No messages yet</p>} | ||
{/* Display the messages */} | ||
{messages | ||
.filter((m) => !!m.trim().length) | ||
.map((message, index) => ( | ||
<p className={'w-full border rounded px-2 py-1'} key={index}> | ||
{message} | ||
</p> | ||
))} | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default Chat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.