Skip to content

Commit

Permalink
https
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-nagaraj committed Sep 24, 2024
1 parent 5e403ad commit 96426c9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
14 changes: 6 additions & 8 deletions client/src/pages/Chat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const Chat = () => {
...res.data,
recipientId: selectedUser._id,
});

}
})
.catch((err) => {
Expand Down Expand Up @@ -103,7 +103,7 @@ const Chat = () => {
} else {
fetchUserChat();
// const newSocket = io("http://localhost:3000");
const newSocket = io("https://we-chat-rho.vercel.app/socket");
const newSocket = io("https://65.2.83.179:3000");
setSocket(newSocket);
return () => newSocket.disconnect();
}
Expand Down Expand Up @@ -159,11 +159,10 @@ const Chat = () => {
return (
<div
key={chat._id}
className={`relative flex flex-row py-4 px-2 items-center border-b-2 border-b-slate-200 dark:border-b-slate-600 ${
selectedUser._id === chat._id
className={`relative flex flex-row py-4 px-2 items-center border-b-2 border-b-slate-200 dark:border-b-slate-600 ${selectedUser._id === chat._id
? "border-l-4 border-blue-400 dark:border-blue-400"
: ""
}`}
}`}
onClick={() => {
setSelectedUser(chat);
fetchChatContext(chat._id);
Expand All @@ -177,11 +176,10 @@ const Chat = () => {
src={`https://ui-avatars.com/api/?name=${spaceToPlus(
chat.name
)}&background=random`}
className={`object-cover h-12 w-12 rounded-full ${
onlineUsers.find((u) => u.userId === chat._id)
className={`object-cover h-12 w-12 rounded-full ${onlineUsers.find((u) => u.userId === chat._id)
? "online"
: ""
}`}
}`}
alt=""
/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion client/src/utils/interceptor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";

// export const baseUrl = "http://localhost:5000/api";
export const baseUrl = "https://we-chat-rho.vercel.app/server/api";
export const baseUrl = "https://65.2.83.179/api";

const axiosInstance = axios.create({
baseURL: baseUrl,
Expand Down
14 changes: 11 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import logger from './logger.js';
import chatRouter from './routes/chatRoute.js';
import messageRouter from './routes/messageRoute.js';
import authenticateToken from './utils/verify.js';
import https from 'https';
import fs from 'fs';

// Read the SSL certificate and key
const privateKey = fs.readFileSync('ssl/server.key', 'utf8');
const certificate = fs.readFileSync('ssl/server.cert', 'utf8');

const credentials = { key: privateKey, cert: certificate };

dotenv.config();

Expand Down Expand Up @@ -35,7 +43,7 @@ app.use(authenticateToken);
app.use('/api/chats', chatRouter);
app.use('/api/messages', messageRouter);


app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
// Create HTTPS server with the credentials
https.createServer(credentials, app).listen(443, () => {
console.log('HTTPS Server running on port 443');
});
34 changes: 27 additions & 7 deletions socket/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import fs from 'fs';
import https from 'https';
import { Server } from "socket.io";
import express from 'express';

const io = new Server({
// cors: "http://localhost:5173/",
cors: "https://we-chat-rho.vercel.app/",
const app = express();

// Load SSL certificate and key
const privateKey = fs.readFileSync('ssl/server.key', 'utf8');
const certificate = fs.readFileSync('ssl/server.cert', 'utf8');
const credentials = { key: privateKey, cert: certificate };

// Create an HTTPS server
const httpsServer = https.createServer(credentials, app);

// Initialize Socket.io with the HTTPS server
const io = new Server(httpsServer, {
cors: {
origin: "https://we-chat-rho.vercel.app/",
methods: ["GET", "POST"],
credentials: true, // If you need to send cookies or authorization headers
},
});

let onlineUsers = [];
Expand All @@ -15,7 +32,7 @@ io.on("connection", (socket) => {
if (!onlineUsers.find((user) => user.userId === userId)) {
onlineUsers.push({ userId, socketId: socket.id });
}
console.log(onlineUsers)
console.log(onlineUsers);
io.emit("onlineUsers", onlineUsers);
});

Expand All @@ -27,11 +44,14 @@ io.on("connection", (socket) => {

socket.on("message", (data) => {
const user = onlineUsers.find((user) => user.userId === data.recipientId);
console.log("sending message", data ,"to", user.socketId);
if (user.socketId) {
console.log("sending message", data, "to", user.socketId);
if (user && user.socketId) {
io.to(user.socketId).emit("message", data);
}
});
});

io.listen(3000);
// Start the HTTPS server on port 3000
httpsServer.listen(3000, () => {
console.log("HTTPS socket Server running on port 3000");
});

0 comments on commit 96426c9

Please sign in to comment.