Skip to content

Commit

Permalink
websocket server
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-chausali committed Oct 16, 2024
1 parent ebf7bd8 commit 7746a2b
Show file tree
Hide file tree
Showing 719 changed files with 128,575 additions and 18 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

Empty file added app/api/user/route.ts
Empty file.
31 changes: 26 additions & 5 deletions app/components/AppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,35 @@
"use client"
import Image from "next/image";
import { buttonVariants } from "@/components/ui/button"
import {signIn, signOut} from 'next-auth/react'
import {signIn, signOut, useSession} from 'next-auth/react'
import roomate from '../icons/roommate.png'

import Link from 'next/link'
import Features from "./Features";
import Navigation from "./Navigation";
import { useEffect, useState } from "react";
export default function AppBar(){
return <div className="flex justify-center items-center">
<div className="flex flex-wrap gap-2 justify-center items-center border border-appbar-border shadow-lg rounded-lg w-fit p-2 m-2">
const session = useSession();
// const[bgColor , setbgColor] = useState('[background:linear-gradient(45deg,#172033,theme(colors.slate.800)_50%,#172033)_padding-box,conic-gradient(from_var(--border-angle),theme(colors.slate.600/.48)_80%,_theme(colors.indigo.500)_86%,_theme(colors.indigo.300)_90%,_theme(colors.indigo.500)_94%,_theme(colors.slate.600/.48))_border-box]');

// useEffect(()=>{
// const handleScroll = ()=>{
// const scrollY = window.scrollY;

// if(scrollY > 50){
// setbgColor('bg-gradient-to-b from-gray-900 to-gray-600 ')
// }else setbgColor('[background:linear-gradient(45deg,#172033,theme(colors.slate.800)_50%,#172033)_padding-box,conic-gradient(from_var(--border-angle),theme(colors.slate.600/.48)_80%,_theme(colors.indigo.500)_86%,_theme(colors.indigo.300)_90%,_theme(colors.indigo.500)_94%,_theme(colors.slate.600/.48))_border-box]')
// }
// window.addEventListener('scroll', handleScroll)

// return () => window.removeEventListener('scroll', handleScroll)
// },[])

console.log("sessino", session);


return <div className="flex justify-center items-center ">
<div className={`w-fit p-2 shadow-lg fixed transition-colors duration-500 [background:linear-gradient(45deg,#172033,theme(colors.slate.800)_50%,#172033)_padding-box,conic-gradient(from_var(--border-angle),theme(colors.slate.600/.48)_80%,_theme(colors.indigo.500)_86%,_theme(colors.indigo.300)_90%,_theme(colors.indigo.500)_94%,_theme(colors.slate.600/.48))_border-box] top-0 left-0 right-0 z-50 mx-auto m-3 flex flex-wrap gap-2 justify-center items-center rounded-2xl border border-transparent animate-border`}>
<div className="flex items-center">
<Image src={roomate} alt="room" className="h-10 w-10 rounded-lg"/>
<span className=" font-semibold font-sans text-xs sm:text-lg">Roomie<span className="text-logo-color-green">Connect</span></span>
Expand All @@ -20,8 +40,9 @@ export default function AppBar(){
<path d="M7.999 0C3.582 0 0 3.596 0 8.032a8.031 8.031 0 0 0 5.472 7.621c.4.074.546-.174.546-.387 0-.191-.007-.696-.011-1.366-2.225.485-2.695-1.077-2.695-1.077-.363-.928-.888-1.175-.888-1.175-.727-.498.054-.488.054-.488.803.057 1.225.828 1.225.828.714 1.227 1.873.873 2.329.667.072-.519.279-.873.508-1.074-1.776-.203-3.644-.892-3.644-3.969 0-.877.312-1.594.824-2.156-.083-.203-.357-1.02.078-2.125 0 0 .672-.216 2.2.823a7.633 7.633 0 0 1 2.003-.27 7.65 7.65 0 0 1 2.003.271c1.527-1.039 2.198-.823 2.198-.823.436 1.106.162 1.922.08 2.125.513.562.822 1.279.822 2.156 0 3.085-1.87 3.764-3.652 3.963.287.248.543.738.543 1.487 0 1.074-.01 1.94-.01 2.203 0 .215.144.465.55.386A8.032 8.032 0 0 0 16 8.032C16 3.596 12.418 0 7.999 0z"></path>
</svg>
</Link>
<Link onClick={()=>signIn()} href="" className={buttonVariants({variant:"login", className:"text-xs"})}>Login</Link>
<Link href="" className={buttonVariants({variant:"outline"})}>SignUp</Link>
{session.data===null && <Link onClick={()=>signIn()} href="" className='transition bg-login-background z-10 relative bg-black/90 border-transparent text-white text-sm md:text-sm font-medium duration-200 rounded-full px-4 py-2 flex items-center justify-center shadow-[0px_-1px_0px_0px_#ffffff40_inset,_0px_1px_0px_0px_#ffffff40_inset]'>Signin</Link>}
{ session.data && <Link href="" onClick={()=>signOut()} className='transition bg-neutral-900 z-10 relative bg-black/90 border-transparent text-white text-sm md:text-sm font-medium duration-200 rounded-full px-4 py-2 flex items-center justify-center shadow-[0px_-1px_0px_0px_#ffffff40_inset,_0px_1px_0px_0px_#ffffff40_inset]'>SignOut</Link>}
</div>
</div>

}
120 changes: 120 additions & 0 deletions app/components/Chat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
"use client"
import { useIsChatOpen, useUser } from "@/store/chatStates"
import { useEffect, useState , useRef} from "react";

export default function Chat(){
const chat = useIsChatOpen((state)=> state.isChatOpen)
const setChat = useIsChatOpen((state)=> state.setIsChatOpen)
const receiversChat = useUser((state)=> state.receiversChat);
const sendersChat = useUser((state)=> state.sendersChat);
const name = useUser((state)=> state.name);
const image = useUser((state)=>state.image);
const setReceiversChat = useUser((state)=> state.setReceiversChat);
const setSendersChat = useUser((state)=> state.setSendersChat);
const [socket, setSocket]= useState<WebSocket | null>(null);
const [latestSenderMessage, setLatestSenderMessages] = useState("")
const [latestReceiverMessage, setLatestReceiverMessages] = useState("");
const inputRef = useRef<HTMLInputElement>(null);
const[mergedChats, setMergedChats] = useState<{message:string , sender:string}[]>([])
useEffect(()=>{
const socket = new WebSocket('ws://localhost:4000');
socket.onopen = ()=>{
console.log('connected on client side');
setSocket(socket);
}

socket.onmessage = (message)=>{
setReceiversChat([...receiversChat, message.data])
setLatestReceiverMessages(message.data);
setMergedChats((prev)=>[...prev, {message:message.data, sender:'receiver'}])
// console.log("message received ", message);
}

setSocket(socket);

return ()=> socket.close();

},[])

// const mergedChats = [];
// const maxLength = Math.max(sendersChat.length, receiversChat.length);

// for (let i = 0; i < maxLength; i++) {
// if (i < sendersChat.length) {
// mergedChats.push({ message: sendersChat[i], sender: "sender" });
// }
// if (i < receiversChat.length) {
// mergedChats.push({ message: receiversChat[i], sender: "receiver" });
// }
// }

console.log("merged",mergedChats)

return <div>
<div id="chat-container" className={` ${chat === false ? 'hidden' : ''} fixed bottom-16 right-4 w-96`}>
<div className="bg-white shadow-md rounded-lg max-w-lg w-full">
<div className="p-4 border-b bg-blue-500 text-white rounded-t-lg flex justify-between items-center">
<img src={image} className="h-10 w-10 rounded-full"/>
<p className="text-lg font-semibold">{name}</p>
<button id="close-chat" onClick={()=> setChat(false)}className="text-gray-300 hover:text-gray-400 focus:outline-none focus:text-gray-400">
<svg xmlns="http://www.w3.org/2000/svg" className="w-6 h-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
</div>
<div id="chatbox" className="p-4 h-80 overflow-y-auto">
{/* <!-- Chat messages will be displayed here --> */}

{
mergedChats.map((chat, index)=>{
return (<div className={`${chat.sender === 'sender' ? 'mb-2 text-right' :'mb-2'}`}>
<p className={`${chat.sender === 'sender' ? 'bg-blue-500 text-white rounded-lg py-2 px-4 inline-block':'bg-gray-200 text-gray-700 rounded-lg py-2 px-4 inline-block'}`}>
{chat.message}
</p>
</div>)
})
}

{/* {latestSenderMessage && <div className="mb-2 text-right">
<p className="bg-blue-500 text-white rounded-lg py-2 px-4 inline-block">{latestSenderMessage}</p>
</div>}
{latestReceiverMessage && <div className="mb-2'">
<p className="bg-gray-200 text-gray-700 rounded-lg py-2 px-4 inline-block">{latestReceiverMessage}</p>
</div>} */}

{/* <div className="mb-2 text-right">
<p className="bg-blue-500 text-white rounded-lg py-2 px-4 inline-block">hello</p>
</div>
<div className="mb-2">
<p className="bg-gray-200 text-gray-700 rounded-lg py-2 px-4 inline-block">This is a response from the chatbot.</p>
</div>
<div className="mb-2 text-right">
<p className="bg-blue-500 text-white rounded-lg py-2 px-4 inline-block">this example of chat</p>
</div>
<div className="mb-2">
<p className="bg-gray-200 text-gray-700 rounded-lg py-2 px-4 inline-block">This is a response from the chatbot.</p>
</div>
<div className="mb-2 text-right">
<p className="bg-blue-500 text-white rounded-lg py-2 px-4 inline-block">design with tailwind</p>
</div>
<div className="mb-2">
<p className="bg-gray-200 text-gray-700 rounded-lg py-2 px-4 inline-block">This is a response from the chatbot.</p>
</div> */}
</div>
<div className="p-4 border-t flex">
<input id="user-input" type="text" placeholder="Type a message" ref={inputRef} className="w-full px-3 py-2 border rounded-l-md focus:outline-none focus:ring-2 focus:ring-blue-500"/>
<button id="send-button" onClick={()=>{
if(inputRef.current && inputRef.current.value.length>0 && socket){
socket.send(inputRef.current.value);
setSendersChat([...sendersChat, inputRef.current.value])
setLatestSenderMessages(inputRef.current.value);
setMergedChats([...mergedChats, {message:inputRef.current.value, sender:"sender"}])
inputRef.current.value = "";
}
}} className="bg-blue-500 text-white px-4 py-2 rounded-r-md hover:bg-blue-600 transition duration-300">Send</button>
</div>
</div>
</div>
</div>
}

26 changes: 26 additions & 0 deletions app/components/Recomended.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

import RecomendedIcon from "../icons/recomendedIcon";

import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import Roomate from "./Roomate";
import Room from "./Room";


export default function Recomended(){
return <div className="flex flex-col items-center justify-center m-4 px-4 bg-white mx-auto my-auto w-full max-w-fit">
<div className="flex items-center justify-center">
<RecomendedIcon/>
<span className="text-3xl mb-4 text-white">Recomended</span>
</div>
<div className="flex items-center justify-center">
<Tabs defaultValue="account" className="w-[400px]">
<TabsList>
<TabsTrigger value="room">Room</TabsTrigger>
<TabsTrigger value="roomate">Roomate</TabsTrigger>
</TabsList>
<TabsContent value="room"><Room/></TabsContent>
<TabsContent value="roomate"><Roomate/></TabsContent>
</Tabs>
</div>
</div>
}
Loading

0 comments on commit 7746a2b

Please sign in to comment.