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

Setup peer2peer replication by using official documentation #4960

Closed
jappyjan opened this issue Sep 20, 2023 · 9 comments
Closed

Setup peer2peer replication by using official documentation #4960

jappyjan opened this issue Sep 20, 2023 · 9 comments

Comments

@jappyjan
Copy link

I dont know if this is the right place to ask for this,
but i have the feeling that the replication section of the documentation is missing critical information which prevents me from using replications.

I want to use the peer2peer replication functionality,
i already setup a signaling server using the provided example code which runs fine,
and a connection to it is established from my app (i get logs for new rooms in the server script)

but thats about it.
in the browser i get a lot of the following errors:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'call')
    at _Peer.Readable (_stream_readable.js:178:10)
    at new Duplex (_stream_duplex.js:51:12)
    at new _Peer (index.js:34:5)
    at connection-handler-simple-peer.ts:55:45
    at Array.forEach (<anonymous>)
    at Socket.<anonymous> (connection-handler-simple-peer.ts:47:25)
    at Emitter.emit (index.mjs:136:20)
    at Socket.emitEvent (socket.js:519:20)
    at Socket.onevent (socket.js:506:18)
    at Socket.onpacket (socket.js:474:22)

and no state sync between two browsers.

this is how i start the replication:

async function initReplicationOfOneCollection(database: RxDatabase, collectionName: RxDBCollectionNames) {
    const collection = database[collectionName];
    if (!collection) {
        console.warn(`Collection ${collectionName} does not exist in database ${database.name}`);
        return;
    }

    const replicationPool = await replicateP2P(
        {
            collection,
            // The topic is like a 'room-name'. All clients with the same topic
            // will replicate with each other. In most cases you want to use
            // a different topic string per user.
            // TODO: make this unique per user
            topic: `<some-prefix>--${database.name}--${collectionName}`,
            secret: '<secret_redacted>',
            /**
             * You need a collection handler to be able to create WebRTC connections.
             * Here we use the simple peer handler which uses the 'simple-peer' npm library.
             * To learn how to create a custom connection handler, read the source code,
             * it is pretty simple.
             */
            connectionHandlerCreator: getConnectionHandlerSimplePeer(
                'ws://localhost:8080',
            ),
        }
    );
    replicationPool.error$.subscribe(error => {
        console.error('replicationPool had an error', error);

        if(
            error.parameters.errors &&
            error.parameters.errors[0] &&
            Number(error.parameters.errors[0].code) === 426
        ) {
            // client is outdated -> enforce a page reload
            location.reload();
        }
    });
}

what am i missing/why is it throwing these errors?

@pubkey
Copy link
Owner

pubkey commented Sep 20, 2023

If you run the unit tests in the browser, do they work for you? Because p2p replication is unit tested in the CI where it works.

@jappyjan
Copy link
Author

jappyjan commented Sep 20, 2023

Might be a dumm question but how would I run them in the browser?
I guess I have to run them in my browser otherwise the my wouldn't be very meaningful since my app is also running in the browser and not in nodejs...

@pubkey
Copy link
Owner

pubkey commented Sep 20, 2023

npm run test:browser:dexie

@jappyjan
Copy link
Author

just ran the tests.

everything succeeded except for this test:

1) should not run first replication when autoStart is set to false replication.test.js other autoStart

@jappyjan
Copy link
Author

Just did some further debugging to understand the error better.
It seams that the dependency "readable-stream" tries to import var Stream = require('./internal/streams/stream');
but looking at the files that are there, there is no such file... maybe im just looking at the wrong spot though...

Bildschirmfoto 2023-09-21 um 10 34 04

@jappyjan
Copy link
Author

could it be this has to do with browserify, since i am using vite and bun to bundle / serve my react app?

@pubkey
Copy link
Owner

pubkey commented Sep 21, 2023

could it be this has to do with browserify, since i am using vite and bun to bundle / serve my react app?

I have no idea, sorry.

@jappyjan
Copy link
Author

turns out this is a problem with the combination of vite and simple-peer.
there is already a related issue on the simple-peer repo, though all presented "solutions" dont work/are incomplete.

i am now forking rxdb and developing an alternative p2p plugin that works with vite :D

@jappyjan
Copy link
Author

feross/simple-peer#823

@pubkey pubkey closed this as completed Oct 2, 2023
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

2 participants