Skip to content

Commit

Permalink
socket rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-nagaraj committed Sep 25, 2024
1 parent 244563b commit 5ca34bd
Show file tree
Hide file tree
Showing 4 changed files with 700 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ jobs:
docker stop server-app || true
docker rm server-app || true
docker run -e PORT=5000 -e MONGO_URI="${{ secrets.MONGO_URI }}" -e JWT_KEY=${{ secrets.JWT_KEY }} -d --name server-app -p 5000:5000 ${{ secrets.ECR_SERVER_REPO_URI }}:latest
docker run -e PORT=433 -e MONGO_URI="${{ secrets.MONGO_URI }}" -e JWT_KEY=${{ secrets.JWT_KEY }} -d --name server-app -p 433:433 ${{ secrets.ECR_SERVER_REPO_URI }}:latest
EOF
25 changes: 13 additions & 12 deletions socket/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import fs from 'fs';
import https from 'https';
import { Server } from "socket.io";
import express from 'express';
import https from "https";
import fs from "fs";
import express from "express";

// Initialize an express 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 };
// SSL certificates
const sslOptions = {
key: fs.readFileSync("/ssl/server.key"),
cert: fs.readFileSync("/ssl/server.cert"),
};

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

// Initialize Socket.io with the HTTPS server
// Initialize Socket.IO with CORS and attach to 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
},
});

Expand All @@ -44,7 +45,7 @@ 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);
console.log("sending message", data, "to", user?.socketId);
if (user && user.socketId) {
io.to(user.socketId).emit("message", data);
}
Expand All @@ -53,5 +54,5 @@ io.on("connection", (socket) => {

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

0 comments on commit 5ca34bd

Please sign in to comment.