Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update socket shared types #47

Merged
merged 9 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions collab_service/package-lock.json

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

2 changes: 1 addition & 1 deletion collab_service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"kafkajs": "^2.2.4",
"mongodb": "^6.9.0",
"mongoose": "^7.5.1",
"peerprep-shared-types": "^1.0.65"
"peerprep-shared-types": "^1.0.69"
},
"devDependencies": {
"@types/express": "^4.17.21",
Expand Down
20 changes: 9 additions & 11 deletions gateway_service/package-lock.json

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

6 changes: 3 additions & 3 deletions gateway_service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node dist/app.js",
"start": "node dist/app.js",
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/app.ts",
"build": "tsc"
"build": "tsc"
},
"author": "",
"license": "ISC",
Expand All @@ -16,7 +16,7 @@
"express": "^4.21.0",
"jsonwebtoken": "^9.0.2",
"kafkajs": "^2.2.4",
"peerprep-shared-types": "^1.0.65",
"peerprep-shared-types": "^1.0.69",
"redis": "^4.7.0",
"socket.io": "^4.8.0",
"socket.io-client": "^4.8.0",
Expand Down
16 changes: 8 additions & 8 deletions gateway_service/src/socketHandlers/callHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export interface CallEventDelegate {

export function setUpCallHandler(socket: Socket, delegate: CallEventDelegate) {
socket.on(ClientSocketEvents.INITIATE_CALL, async (data) => {
const { roomId, from, signalData } = data;
console.log("Initiating call in room:", data);
const { roomId, username, signalData } = data;
console.log("Initiating call in room:", roomId, username);

const event = createEvent(CollaborationEvents.CALL, {
roomId: roomId,
from: from,
from: username,
signalData: signalData,
});

Expand All @@ -34,12 +34,12 @@ export function setUpCallHandler(socket: Socket, delegate: CallEventDelegate) {
});

socket.on(ClientSocketEvents.ACCEPT_CALL, async (data) => {
const { roomId, from, signalData } = data;
console.log("Accepting call in room:", data);
const { roomId, username, signalData } = data;
console.log("Accepting call in room:", roomId, username);

const event = createEvent(CollaborationEvents.ACCEPT_CALL, {
roomId: roomId,
from: from,
from: username,
signalData: signalData,
});

Expand All @@ -48,12 +48,12 @@ export function setUpCallHandler(socket: Socket, delegate: CallEventDelegate) {
});

socket.on(ClientSocketEvents.END_CALL, async (data) => {
const { roomId, from } = data;
const { roomId, username } = data;
console.log("Ending call in room:", data);

const event = createEvent(CollaborationEvents.END_CALL, {
roomId: roomId,
from: from,
from: username,
});

// send event to collaboration service
Expand Down
2 changes: 1 addition & 1 deletion gateway_service/src/socketHandlers/chatHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function setUpChatHandler(socket: Socket, delegate: ChatEventDelegate) {
await delegate(event, roomId);
});

socket.on(ClientSocketEvents.CHAT_STATE, async (data) => {
socket.on(ClientSocketEvents.GET_CHAT_STATE, async (data) => {
const { roomId } = data;
console.log("Requesting chat state for room:", roomId);

Expand Down
48 changes: 33 additions & 15 deletions gateway_service/src/socketHandlers/roomHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
createEvent,
EventPayloads,
KafkaEvent,
ServerSocketEvents,
} from "peerprep-shared-types";
import { CollaborationEvents } from "peerprep-shared-types/dist/types/kafka/collaboration-events";
import { Socket } from "socket.io";
Expand Down Expand Up @@ -40,7 +41,9 @@ export function setupRoomHandler(socket: Socket, delegate: RoomEventDelegate) {

socket.on(ClientSocketEvents.LEAVE_ROOM, async (data) => {
console.log("Leaving room:", data.roomId);
socket.to(data.roomId).emit(ClientSocketEvents.LEAVE_ROOM, data.username);
socket.to(data.roomId).emit(ServerSocketEvents.USER_LEFT, {
username: data.username,
});
socket.leave(data.roomId);

const event = createEvent(CollaborationEvents.LEAVE_ROOM, {
Expand All @@ -52,39 +55,54 @@ export function setupRoomHandler(socket: Socket, delegate: RoomEventDelegate) {
await delegate(event, data.roomId);
});

socket.on(ClientSocketEvents.CODE_CHANGE, async (data) => {
const { roomId, username, message } = data;
console.log("Code change in room:", message);
socket.on(ClientSocketEvents.CHANGE_CODE, async (data) => {
const { roomId, username, sharedCode } = data;
console.log("Code change in room:", data);

// everyone in the room except the sender will receive the code change on frontend
socket.to(roomId).emit(ClientSocketEvents.CODE_CHANGE, {
socket.to(roomId).emit(ServerSocketEvents.CODE_CHANGED, {
username,
roomId,
content: message.sharedCode,
language: message.language,
sharedCode: sharedCode,
timestamp: Date.now(),
});

const event = createEvent(CollaborationEvents.UPDATE_CODE, {
roomId: data.roomId,
username: data.username,
content: data.message.sharedCode,
roomId: roomId,
username: username,
content: sharedCode,
});

// send event to collaboration service
await delegate(event, roomId);
});

socket.on(ClientSocketEvents.CHANGE_LANGUAGE, async (data) => {
const { roomId, username, language } = data;
console.log("Language change in room:", data);

socket.to(roomId).emit(ServerSocketEvents.LANGUAGE_CHANGED, {
username,
roomId,
language,
});
});

// Handle next question that has just been initiated by a user
socket.on(ClientSocketEvents.NEXT_QUESTION, async (data) => {
const { roomId, username, accept } = data;
socket.on(ClientSocketEvents.REQUEST_NEXT_QUESTION, async (data) => {
const { roomId, username } = data;
console.log("Requesting next question for room:", roomId);

// everyone in the room except the sender will receive request for next question on frontend
// send the event to everyone in the room
socket.to(roomId).emit(ServerSocketEvents.NEXT_QUESTION_REQUESTED, {
username: username,
roomId: roomId,
});

const event = createEvent(CollaborationEvents.NEXT_QUESTION, {
roomId: roomId,
username: username,
accept: accept,
accept: true,
});

// send event to collaboration service
Expand All @@ -104,7 +122,7 @@ export function setupRoomHandler(socket: Socket, delegate: RoomEventDelegate) {
username
);

socket.to(roomId).emit(ClientSocketEvents.REPLY_NEXT_QUESTION, {
socket.to(roomId).emit(ServerSocketEvents.NEXT_QUESTION_REPLIED, {
username: username,
roomId: roomId,
accept: accept,
Expand Down
13 changes: 6 additions & 7 deletions gateway_service/src/websocket-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export class WebSocketHandler {
console.log("Room state refresh event received:", roomPayload);

console.log("Broadcasting code change:");
// socket.emit("roomUpdated", {});
this.io.to(roomPayload.roomId).emit("roomUpdated", {
room: roomPayload.editorState,
});
Expand All @@ -162,7 +161,7 @@ export class WebSocketHandler {
);
this.io
.to(newChatPayload.roomId)
.emit(ClientSocketEvents.NEW_CHAT, newChatPayload.message);
.emit(ServerSocketEvents.NEW_CHAT, newChatPayload.message);
break;
case GatewayEvents.REFRESH_CHAT_STATE:
const chatStatePayload =
Expand All @@ -174,7 +173,7 @@ export class WebSocketHandler {
);
this.io
.to(chatStatePayload.roomId)
.emit(ClientSocketEvents.CHAT_STATE, {
.emit(ServerSocketEvents.CHAT_STATE, {
messages: chatStatePayload.chatState.messages,
});
break;
Expand Down Expand Up @@ -209,7 +208,7 @@ export class WebSocketHandler {
);
this.io
.to(changeQuestionPayload.roomId)
.emit(ClientSocketEvents.QUESTION_CHANGE, {
.emit(ServerSocketEvents.QUESTION_CHANGED, {
questionId: changeQuestionPayload.questionId,
});
break;
Expand All @@ -219,7 +218,7 @@ export class WebSocketHandler {
console.log("Sending call to user:", callPayload.to);
const toSocketId = await this.getUsernameSocketId(callPayload.to);
if (toSocketId) {
this.io.to(toSocketId).emit(ClientSocketEvents.INITIATE_CALL, {
this.io.to(toSocketId).emit(ServerSocketEvents.CALL_REQUESTED, {
from: callPayload.from,
signalData: callPayload.signalData,
});
Expand All @@ -235,7 +234,7 @@ export class WebSocketHandler {
acceptCallPayload.to
);
if (acceptSocketId) {
this.io.to(acceptSocketId).emit(ClientSocketEvents.ACCEPT_CALL, {
this.io.to(acceptSocketId).emit(ServerSocketEvents.CALL_ACCEPTED, {
from: acceptCallPayload.from,
signalData: acceptCallPayload.signalData,
});
Expand All @@ -249,7 +248,7 @@ export class WebSocketHandler {
console.log("Ending call with user:", endCallPayload.to);
const endSocketId = await this.getUsernameSocketId(endCallPayload.to);
if (endSocketId) {
this.io.to(endSocketId).emit(ClientSocketEvents.END_CALL, {
this.io.to(endSocketId).emit(ServerSocketEvents.CALL_ENDED, {
from: endCallPayload.from,
});
} else {
Expand Down
8 changes: 4 additions & 4 deletions matching_service/package-lock.json

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

2 changes: 1 addition & 1 deletion matching_service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"kafkajs": "^2.2.4",
"mongodb": "^6.9.0",
"mongoose": "^7.5.1",
"peerprep-shared-types": "^1.0.65",
"peerprep-shared-types": "^1.0.69",
"redis": "^4.7.0",
"socket.io": "^4.8.0",
"socketio-jwt": "^4.6.2",
Expand Down
2 changes: 1 addition & 1 deletion peerprep-shared-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "peerprep-shared-types",
"version": "1.0.65",
"version": "1.0.69",
"description": "Shared TypeScript types for PeerPrep frontend and backend",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
Loading