Skip to content

Commit

Permalink
Fix connection between FE and Yjs (#133)
Browse files Browse the repository at this point in the history
* Install ESLint for Github Action

* Fix issue connecting to yjs from fe
  • Loading branch information
evanyan13 authored Nov 13, 2024
1 parent e7b0fdc commit adc452f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-build-push-gcloud.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ jobs:
# Build and push the Docker image
echo "Building and pushing image for $service..."
docker buildx build --platform linux/amd64 -t $IMAGE_NAME -f ./backend/$service/Dockerfile ./backend/$service --push
docker build --platform linux/amd64 -t $IMAGE_NAME -f ./backend/$service/Dockerfile ./backend/$service --push
done
25 changes: 18 additions & 7 deletions backend/y-websocket-service/src/yjs.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ export class YjsGateway implements OnGatewayConnection, OnGatewayDisconnect {

async handleConnection(client: WebSocket, request: Request) {
try {
console.log('New connection:', request.url);
const url = new URL(request.url, 'http://${request.headers.host}');
const sessionId = url.searchParams.get('sessionId');
// roomId is appended to the end of the URL like so /yjs?sessionId=123&userId=456/roomId789
// Thus the reason to split the userIdParam by '/' to get the userId and roomId
// Very hacky. App might break if param order changes
const userIdParam = url.searchParams.get('userId');
const userId = userIdParam.split('/')[0];
const roomId = userIdParam.split('/')[1];

setupWSConnection(client, request, { docName: roomId, gc: true });
if (!userIdParam || !userIdParam.includes('/')) {
console.error('Invalid user ID parameter');
client.close(1008, 'Invalid user ID parameter');
return;
}

const [userId, roomId] = userIdParam.split('/');

if (!sessionId) {
console.error('No session ID provided');
Expand All @@ -52,7 +54,16 @@ export class YjsGateway implements OnGatewayConnection, OnGatewayDisconnect {
return;
}

console.log('Session ID:', sessionId, 'User ID:', userId);
console.log(
'Session ID:',
sessionId,
'User ID:',
userId,
'Room ID:',
roomId,
);

setupWSConnection(client, request, { docName: roomId, gc: true });

const sessionDetails = await this.validateSessionDetails(
sessionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ export default function CollaborativeEditor({
minimap: { enabled: false },
}}
defaultValue={defaultEditorValues[language]}
// value={code[language]}
// onChange={(value) => {
// setCode((prevCode) => ({ ...prevCode, [language]: value || "" }));
// }}
// value={code[language]}
// onChange={(value) => {
// setCode((prevCode) => ({ ...prevCode, [language]: value || "" }));
// }}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useSessionContext } from "@/contexts/SessionContext";
export default function CollaborativeEditorTab() {
const {sessionId, userProfile} = useSessionContext();

const socketUrl = process.env.PUBLIC_Y_WEBSOCKET_URL || "ws://localhost:4001";
const socketUrl = process.env.NEXT_PUBLIC_Y_WEBSOCKET_URL || "ws://localhost:4001";

if (!userProfile) {
return <div>Loading user profile...</div>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import CollaborativeWhiteboard from "./CollaborativeWhiteboard";
import { useSessionContext } from "@/contexts/SessionContext";

export default function CollaborativeEditorTab() {
const {userProfile, sessionId} = useSessionContext()
const { userProfile, sessionId } = useSessionContext()

const socketUrl = process.env.PUBLIC_Y_WEBSOCKET_URL || "ws://localhost:4001";
const socketUrl = process.env.NEXT_PUBLIC_Y_WEBSOCKET_URL || "ws://localhost:4001";

if (!userProfile) {
return <div>Loading user profile...</div>;
Expand Down

0 comments on commit adc452f

Please sign in to comment.