gondar00 / yjs-websocket-server-with-nestjs

A simple nestjs server with yjs integration
15 stars 1 forks source link

Feature request: support of rooms #1

Open minchopm opened 1 year ago

minchopm commented 1 year ago

Is there any way to implement support of rooms

gondar00 commented 1 year ago

The second parameter here while creating the y-websocket connection is the room name

jblyberg commented 1 year ago

When I pass a room name, I'm getting, Firefox can’t establish a connection to the server at ws://localhost:5001/testroom.

It seems that the connection request will not even pass handleConnection() in the nest gateway.

gondar00 commented 1 year ago

@jblyberg you're right, I found another way where you can set the roomName in the cookie and connect users to different yjs documents based on that value

minchopm commented 1 year ago

You could pass room name as param

new WebsocketProvider(this.socketPath, '', yDoc, {
     params: {'roomName': subDoc.guid}
});

and read it

const parsedUrl = url.parse(req.url);
const parsedQs = querystring.parse(parsedUrl.query);
if(parsedQs.roomName){
    const docName = parsedQs.roomName;
    setupWSConnection(connection, req, { ...(docName && { docName }) });
}

if you handle multiple Web socket connections on same page, will be difficult to manage their rooms

titanbit commented 10 months ago

Is there any way to connect with nestjs application with dynamic roomIds, my client side is running on react and backend is running on nestjs. but unable to connect to the room id.

frontend code : provider.current = new WebsocketProvider( ws://localhost:3030/ws/, // Replace with your WebSocket server URL ${fileId}, ydoc.current, )

Backend :

import { WebSocketGateway, OnGatewayConnection, OnGatewayDisconnect, WebSocketServer, } from '@nestjs/websockets'; // import { WebSocketServer } from 'ws'; import * as Y from 'yjs'; import { setupWSConnection } from 'y-websocket/bin/utils'; import { Server, WebSocket } from 'ws';

@WebSocketGateway({ cors: true }) export class AppGateway implements OnGatewayConnection, OnGatewayDisconnect { @WebSocketServer() server: Server;

handleConnection(client: WebSocket, ...args: any[]): void { console.log('connect'); const request = args[0]; const fileId = this.extractFileId(request.url);

// Create a new Yjs document or get an existing one
const ydoc = new Y.Doc();

// Setup WebSocket connection for Yjs
setupWSConnection(client, request, ydoc, {
  gc: true, // garbage collection
});

console.log(`Client connected for document ID: ${fileId}`);

}

handleDisconnect(client: WebSocket): void { console.log('Client disconnected'); }

private extractFileId(url: string): string { // Extract fileId from URL return url.split('/').pop(); } } but not connected. is there any issue while using this with different servers.

one more thing when using provider.current = new WebsocketProvider( wss://demos.yjs.dev/ws/, // Replace with your WebSocket server URL ${fileId}, ydoc.current, )

  its working.