hypercore: Failed to get hypercore to replicate between server and browser.
I have tried several different stream mechanisms to try and get the hypercore to replicate from browser to a nodejs server. This last one I tried was websockets and libraries that stream them. I also tried extensively socket.io-stream and hyperswarm-web (I can supply code of these attempts too). Thus I’m out of ideas on transporting hypercore between browser and server through stream mechanisms.
Here is the code I’m using for the websockets (which works on other “non hyper” streams I’m piping to server from the browser, just not hypercore). I get no errors on either the client or server side to help me navigate what the problem could be.
Server
const core = new Hypercore("./resource");
await core.ready();
console.log(core.key.toString("hex"));
if (core.length <= 3) {
core.append("this is a cool thing");
}
// wss is 'ws/WebSocketServer' npm library. I've also tried 'websocket-stream' createServer method with same problem.
wss.on("connection", (ws, req, client) => {
const stream = createWebSocketStream(ws);
stream.pipe(core.replicate(false)).pipe(stream);
});
http.on("upgrade", (req, socket, head) =>
{
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit("connection", ws, req, {});
})
});
Server Core Key: 8ae30dc381cb254016386754f095594d91719060f8d7f02804015dece65a027a
Client/Browser
const core = new Hypercore(() => new RAM(),
Buffer.from("8ae30dc381cb254016386754f095594d91719060f8d7f02804015dece65a027a", "hex"));
// ws is "websocket-stream" npm library
const socket = ws(/** omitted address **/);
await Promise.all([
new Promise(resolve => socket.once("connect", resolve)),
core.ready()
]);
addMessage("Core key: " + core.key.toString("hex"));
addMessage("Websocket Connected");
addMessage("Piping hypercore to server.");
socket.pipe(core.replicate(true)).pipe(socket);
addMessage("Trying to update the core.");
addMessage("Core updated: " + await core.update());
addMessage("Trying to get core info.");
const info = await core.info();
console.log("Core info:", info);
addMessage("Core info in console.");
//const rng = core.download({start: 1, end: 2});
//await rng.done(); // <-- this fails with "done is not a function"
//addMessage("Trying to download core range [1,2].");
addMessage("Getting index 1 of core");
await core.get(1, {
onwait() {
addMessage("Waiting for core[1] to be downloaded..");
}
});
addMessage("Getting index 2 of core");
await core.get(2, {
onwait() {
addMessage("Waiting for core[2] to be downloaded..");
}
});
Browser Messages:
Core key: 8ae30dc381cb254016386754f095594d91719060f8d7f02804015dece65a027a
Websocket Connected
Piping hypercore to server.
Trying to update the core.
Core updated: false
Trying to get core info.
Core info in console. --> {"length": 0,"contiguousLength": 0,"byteLength": 0,"padding": 0}
Getting index 1 of core
Waiting for core[1] to be downloaded..
Now, regular append/get of hypercore in browser without server works:
const core = new Hypercore(() => new RAM(), {
valueEncoding: "json"
});
await core.ready();
const {length} = await core.append({
hello: "world!"
});
addMessage("Appended to: " + (length - 1));
const {hello} = await core.get(length - 1);
addMessage("Data got from core hello: " + hello);
Browser Messages:
Appended to: 0
Data got from core hello: world!
Any help or advice to navigate the replicate stream problem from browser would be appreciated 😃.
- Zack
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (18 by maintainers)
With your suggestions I have managed to replicate the hypercore to the browser through a hyperswarm tooled with the dht-relay. I will prepare an example of my code for anyone else that visits this issue and close this issue with it.
Thank you for your help LuKks. I greatly appreciate your guidance through this matter.
Try setting up a
dht-relayserver using WebSocket: https://github.com/hyperswarm/dht-relay When you have that running then use that relay server i.e. ws://1.2.3.4:8080For example, create a DHT node where you do
node.createServer()so you can replicate from there as you already did. Later, from browser use the samedht-relaylibrary to connect to the WebSocket relay, so you can easily create aDHTinstance to donode.connect(serverPublicKey)and easily receive the replication.My working setup for replicating hypercore over hyperswarm (https://github.com/hyperswarm/hyperswarm) between browser and node server using dht-relay (https://github.com/hyperswarm/dht-relay).
Server Example:
I used browserify to pack Hypercore, Hyperswarm, dht-relay using an html import map to load the scripts. I was also testing with different device types (Android/iPhone) so I displayed result to the page for convivence.
Client/Browser:
Ah, very true, I was missing the dht option. So the browser it’s like this:
Node.js
Browser:
(this works but it’s not the best or correct way to do it)
Node.js:
Browser (I say React because I don’t know the effects of browserify vs webpack or whichever React uses):
Worst case scenario, create a repo to reproduce the issue so I can try
Otherwise, servers/clients may not found themselves because they would be in completely different setups/networks, but you will notice it so no problem
Cool, let me know!
Keep in mind that my DHT network is isolated from the mainnet network, that means if you use my relay then you have to set the bootstrap list.
For example, in Node.js:
In the browser:
But you can setup a relay server using the default bootstrap list (i.e. not setting the
bootstrapvariable)!