PrismarineJS / flying-squid

Create Minecraft servers with a powerful, stable, and high level JavaScript API.

Home Page:https://prismarinejs.github.io/flying-squid/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[feat req] Use in browser via webpack, browserify

redbrain opened this issue · comments

Hello,
It would be nice if flying-squid would be webpackable to use completely client-side.
What currently limits flying-squid from being webpacked, and can these limitations be ignored to create a simple server?

yeah totally agree, that's something we want for PrismarineJS/prismarine-web-client#47

Nobody tried to do it but the minimum we need is a connection provider in node-minecraft-protocol server to replace the tcp server. Opened an issue for this there PrismarineJS/node-minecraft-protocol#829

Good to hear that this is already in consideration.
I'll subscribe to the mentioned issues, and speak here again once those are mature enough to implement a client-side server.

In addition to a TCP alternative, we'll need to provide an alternative to FS, since browsers don't have native FS access (yet).
For player data, perhaps the browser's localStorage is a good alternative; that will probably need an option to be enabled.
The items exported via requireindex could probably be specified manually, at least to my understanding.
I'm not sure how we should implement world storage and logging. I also haven't looked at FS usage in any dependencies.

Yeah world storage can be done using indexeddb

BrowserFS would be a good way to access FS in browser.

Maybe WebSockets for the client communication?

No network connection is necessary in single player mode

No network connection is necessary in single player mode

Fair, we could use a one-sided messaging channel with some javascript, like the way socket.io works for messages.

All that needs to be done is use the existing event emitters. Drop nmp dep and when mineflayer does a write, the event should instead be emitted to flying squid directly.

No serialization is needed, and when flying squid wants to write it would just go right to mineflayer bot eventemitter

webStorage isn't an actual fs, it is emulated, and indexedDB is much faster, and with a Blob

But does it even make sense to webpack it???

I've worked on wrapping flying-squid to let it run in the browser over at https://github.com/vantezzen/electric-squid (https://squid.blymp.io/). It's still a very rough proof-of-concept, but connecting to the server via a Minecraft Client is still (mostly) possible.

There were some difficulties getting webpack to build the bundle (mainly solved by increasing the RAM limit, writing custom polyfills and patching dependencies manually) but generally it seems do-able. webpack's tree-shaking also seems to be very stressed as some small changes to the code result in the bundle size fluctuating between 50-400MB.

I've used the methods discussed here to create a client-server connection via WebSockets and using IndexedDB via the level filesystem to replace the fs module.

@vantezzen that's awesome!
Let's us know how it goes
If you see changes in flying-squid that could make this experiment easier, do say

commented

Hi everyone! I couldn't find a way to polyfill all the necessary components, so I decided to debug and modify the required code instead.
Right now, I've managed to use this server in the browser and implemented the desired singleplayer support in forked mineflayer web client. In the browser, we can easily save worlds in browser memory or interact with the real file system instead (however the sad part is that Chrome doesn't allow to open folders in %appdata% directly). I don't have a clean solution yet, though there are already some improvements in the fork

commented

If you can figure out some branching so that both in browser and in node is supported with the same code and contribute to this repo it would be great.

Sure thing! No worries at all, as long as you're up for reviewing the code :)