Skip to content

Commit

Permalink
initial socket io for messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jitunayak committed Aug 25, 2024
1 parent 9a02ebf commit 61c14e3
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-shimmer-effects": "^1.0.3",
"socket.io-client": "^4.7.5",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
75 changes: 75 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/components/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export default function UserMenu({
const { logout } = useKindeAuth();
return (
<FloatWrapper ref={menuRef}>
<MenuItem variant="primary">Messages</MenuItem>
<MenuItem to="/messages" variant="primary">
Messages
</MenuItem>
<MenuItem variant="primary">Notifications</MenuItem>
<MenuItem to="/profile/bookings" variant="primary">
Bookings
Expand Down
77 changes: 77 additions & 0 deletions src/pages/Messages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Row } from '@/components';
import { Button } from '@/components/Button';
import { socket } from '@/socket';
import { SendIcon } from 'lucide-react';
import React, { useEffect, useState } from 'react';

export const Messages: React.FC = () => {
const [inputText, setInputText] = useState('');
const [isConnected, setIsConnected] = useState(socket.connected);
const [messages, setmessages] = useState<string[]>([]);

useEffect(() => {
function onConnect() {
setIsConnected(true);
}

function onDisconnect() {
setIsConnected(false);
}

function onMessageEvent(value: string) {
setmessages((previous) => [...previous, value]);
}

socket.on('connect', onConnect);
socket.on('disconnect', onDisconnect);
socket.on('message', onMessageEvent);

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

return (
<div>
<h1>Messages</h1>
<div>
<p>Is connected: {isConnected ? 'Yes' : 'No'}</p>
<p>Number of events: {messages.length}</p>
</div>

{messages.map((event) => (
<p key={event}>{event}</p>
))}

<Row style={{ gap: '.6rem' }}>
<input
style={{
border: '1px solid #ccc',
borderRadius: '6px',
fontSize: '16px',
padding: '6px',
}}
onChange={(e) => setInputText(e.target.value)}
type="text"
value={inputText}
/>
<Button
onClick={() => {
socket.emit('message', inputText);
setmessages((previous) => [...previous, inputText]);
setInputText('');
}}
color={'primary'}
round={'xs'}
>
<SendIcon size={16} />
Send
</Button>
</Row>
</div>
);
};

export default Messages;
18 changes: 18 additions & 0 deletions src/routeTree.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createFileRoute } from '@tanstack/react-router'
// Import Routes

import { Route as rootRoute } from './routes/__root'
import { Route as MessagesImport } from './routes/messages'
import { Route as IndexImport } from './routes/index'
import { Route as RoomsRoomIdImport } from './routes/rooms/$roomId'

Expand Down Expand Up @@ -40,6 +41,11 @@ const LoginLazyRoute = LoginLazyImport.update({
getParentRoute: () => rootRoute,
} as any).lazy(() => import('./routes/login.lazy').then((d) => d.Route))

const MessagesRoute = MessagesImport.update({
path: '/messages',
getParentRoute: () => rootRoute,
} as any)

const IndexRoute = IndexImport.update({
path: '/',
getParentRoute: () => rootRoute,
Expand Down Expand Up @@ -93,6 +99,13 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof IndexImport
parentRoute: typeof rootRoute
}
'/messages': {
id: '/messages'
path: '/messages'
fullPath: '/messages'
preLoaderRoute: typeof MessagesImport
parentRoute: typeof rootRoute
}
'/login': {
id: '/login'
path: '/login'
Expand Down Expand Up @@ -156,6 +169,7 @@ declare module '@tanstack/react-router' {

export const routeTree = rootRoute.addChildren({
IndexRoute,
MessagesRoute,
LoginLazyRoute,
WishlistsLazyRoute,
RoomsRoomIdRoute,
Expand All @@ -175,6 +189,7 @@ export const routeTree = rootRoute.addChildren({
"filePath": "__root.tsx",
"children": [
"/",
"/messages",
"/login",
"/wishlists",
"/rooms/$roomId",
Expand All @@ -188,6 +203,9 @@ export const routeTree = rootRoute.addChildren({
"/": {
"filePath": "index.tsx"
},
"/messages": {
"filePath": "messages.tsx"
},
"/login": {
"filePath": "login.lazy.tsx"
},
Expand Down
6 changes: 6 additions & 0 deletions src/routes/messages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Messages from '@/pages/Messages';
import { createFileRoute } from '@tanstack/react-router';

export const Route = createFileRoute('/messages')({
component: () => <Messages />,
});
6 changes: 6 additions & 0 deletions src/socket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { io } from 'socket.io-client';

// "undefined" means the URL will be computed from the `window.location` object
const URL = import.meta.env.VITE_BASE_URL as string;

export const socket = io(URL);

0 comments on commit 61c14e3

Please sign in to comment.