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

Feature request: support of rooms #1

Open
minchopm opened this issue Nov 23, 2022 · 5 comments
Open

Feature request: support of rooms #1

minchopm opened this issue Nov 23, 2022 · 5 comments

Comments

@minchopm
Copy link

Is there any way to implement support of rooms

@gondar00
Copy link
Owner

gondar00 commented Dec 6, 2022

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

@jblyberg
Copy link

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
Copy link
Owner

@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
Copy link
Author

minchopm commented Feb 1, 2023

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
Copy link

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. 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants