Giter Club home page Giter Club logo

iogrid's Introduction

IOGrid

Join the chat at https://gitter.im/SocketCluster/iogrid

IOGrid is an IO game engine/framework built using SocketCluster and Phaser. It lets you build multi-player games like Agar.io and Slither.io and also multi-player simulations/experiments for research purposes. It is designed to scale across multiple processes to make use of all CPU cores on a machine.

The game world is divided into cells which will be distributed across available SC worker processes. Basic initial tests indicate that this engine can scale linearly across available CPU cores - I've found that doubling the number of worker processes allowed the engine to handle approximately double the number of bots whilst maintaining the average CPU usage per worker process at 50%.

Each cell in the world has its own instance of a cell controller (cell.js) - Ideally, this is where you should put all your back end game logic. If you follow some simple structural guidelines, your code should automatically scale. With this approach, you should be able to build very large worlds which can host thousands of concurrent players.

If you've built a game using this engine, feel free to contribute back to this repo. Also, feel free to get in touch with me directly by email (see my GitHub profile http://github.com/jondubois) if you'd like to chat, have feedback, need advice or need help with a project.

IOGrid demo

Special thanks to the Percepts and Concepts Laboratory at Indiana University (http://cognitrn.psych.indiana.edu/) for sponsoring this project.

Developing

The front-end code is in public/index.html, the back-end code is in worker.js and cell.js. Read the comments in the code for more details about how it all works.

Running

To run on your machine, you need to have Node.js v6.0.0 or higher installed. Then you can either clone this repo with Git using the command:

git clone [email protected]:SocketCluster/iogrid.git

... Or you can download the zip: https://github.com/SocketCluster/iogrid/archive/master.zip and extract it to a directory of your choice.

Once you have this repo setup in a iogrid directory on your machine, you need to navigate to it using the terminal and then run:

npm install

Then (while still inside the iogrid directory) you can launch the SocketCluster server using:

node server

To run the demo, navigate to http://localhost:8000 in a browser - You should see a rabbit which you can move around using the arrow keys.

To test the multi-player functionality from your localhost:

Open up another browser window/tab to http://localhost:8000 and put it side-by-side with the first window/tab - You should now have two rabbits - Each one can be controlled from a different tab.

Note that while this demo demonstrates a few important optimizations, it can still be optimized further. For production usage, among other things, you may want to improve the current codec to make the packets that are sent to the client even smaller. You may want to build your own codec on top of https://github.com/SocketCluster/sc-codec-min-bin.

If you want to run the server on port 80, you'll need to run the SocketCluster server with sudo node server -p 80.

For more info about SocketCluster, visit http://socketcluster.io/.

If you want to find out more about authentication and authorization, you may want to look into SC middleware: http://socketcluster.io/#!/docs/middleware-and-

To run the engine on multiple CPU cores, you just need to add more worker and broker processes. You can do this by adding extra parameters to the node server command (-w is the number of worker processes and -b is the number of broker processes):

node server -w 3 -b 1

Unless your CPU/OS is particularly efficient with multitasking, you generally want to have one process per CPU core (to avoid sharing cores/context switching penalties). Note that in the example above, we are launching 4 processes in total; 3 workers and 1 broker.

Deciding on the correct ratio of workers to brokers is a bit of a balancing act and will vary based on your specific workload - You will have to try it out and watch your processes. When you launch the engine, SocketCluster will tell you the PIDs of your worker and broker processes.

Based on the rudimentary tests that I've carried out so far, I've found that you generally need more workers than brokers. The ratio of workers to brokers that seems to work best for most use cases is approximately 2:1.

Also note that cell controllers (cell.js) will be evenly sharded across available workers. For this reason, it is highly recommended that you divide your world grid in such a way that your number of worker processes and total number of cells share a common factor. So for example, if you have 3 workers, you can have a world grid with dimensions of 3000 * 3000 pixels made up of 3 cells of dimensions 1000 * 3000 (rectangular cells are fine; in fact, I highly encourage them since they are more efficient).

More Info

It's still very early for this project, here are some things that still need improving:

  • The front end needs some sort of motion smoothing since we don't want to set the WORLD_UPDATE_INTERVAL too high (for bandwidth reasons) and so the animation should be smoothed out on the front end.
  • The front end needs an overall cleanup; maybe we need to move the core logic outside of index.html into its own .js file... And maybe we can start using the import statement (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) to load dependencies?
  • We need to make a custom SocketCluster codec specifically for this game engine to compress all outgoing messages to be as small as possible. Right now it's just using a general-purpose binary compression codec for SC - We should add another codec layer on top of this.

License

MIT

iogrid's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

iogrid's Issues

Error when starting testing/development serve.

1531271367807 - Worker 0 exited - Exit code: 1
1531271368185 - Origin: Worker (PID 9920)
[Error] Error: Compilation of µWebSockets has failed and there is no pre-compiled binary available for your system. Please install a supported C++11 compiler and reinstall the module 'uws'.
at native (C:\Users\Drew\Desktop\Django\Queen.io\node_modules\uws\uws.js:38:19)
at Object. (C:\Users\Drew\Desktop\Django\Queen.io\node_modules\uws\uws.js:42:3)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at new SCServer (C:\Users\Drew\Desktop\Django\Queen.io\node_modules\socketcluster-server\scserver.js:97:18)
1531271368193 - Worker 0 exited - Exit code: 1
1531271368630 - Worker 0 exited - Exit code: 3221225786
1531271368646 - Origin: BrokerEngine (PID 4880)
[Error] ProcessExitError: scBroker server at socket path \.\pipe\socketcluster_e24aeafe-1ba5_02a88a8eb6_b0 exited
at Server. (C:\Users\Drew\Desktop\Django\Queen.io\node_modules\sc-broker-cluster\index.js:371:19)
at emitOne (events.js:116:13)
at Server.emit (events.js:211:7)
at Server.EventEmitter.emit (C:\Users\Drew\Desktop\Django\Queen.io\node_modules\sc-domain\index.js:12:31)
at ChildProcess. (C:\Users\Drew\Desktop\Django\Queen.io\node_modules\sc-broker\index.js:82:10)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at ChildProcess.EventEmitter.emit (C:\Users\Drew\Desktop\Django\Queen.io\node_modules\sc-domain\index.js:12:31)
at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)

help

Debugger listening on [::]:15454
module.js:327
throw err;
^

Error: Cannot find module './lib/nodefs-handler'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object. (/home/ubuntu/workspace/node_modules/chokidar/index.js:12:21)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)

Process exited with code: 1

basic start up error

npm ERR! EEXIST, mkdir '/root/speesmane/node_modules/socketcluster/node_modules/ inquirer/node_modules/lodash'
File exists: /root/speesmane/node_modules/socketcluster/node_modules/inquirer/no de_modules/lodash
Move it away, and try again.

npm ERR! System Linux 3.16.0-25-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /root/speesmane
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.4.21
npm ERR! path /root/speesmane/node_modules/socketcluster/node_modules/inquirer/n ode_modules/lodash
npm ERR! fstream_path /root/speesmane/node_modules/socketcluster/node_modules/in quirer/node_modules/lodash/isArrayLike.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code EEXIST
npm ERR! errno 47
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:171:23
npm ERR! fstream_stack /usr/lib/nodejs/mkdirp/index.js:46:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! Error: ENOENT, open '/root/speesmane/node_modules/socketcluster/node_mo dules/inquirer/node_modules/rx/dist/rx.core.testing.js'
npm ERR! If you need help, you may report this entire log,
npm ERR! including the npm and node versions, at:
npm ERR! http://github.com/npm/npm/issues

npm ERR! System Linux 3.16.0-25-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /root/speesmane
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.4.21
npm ERR! path /root/speesmane/node_modules/socketcluster/node_modules/inquirer/n ode_modules/rx/dist/rx.core.testing.js
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! [email protected] postinstall: node postinstall
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is most likely a problem with the spawn-sync package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node postinstall
npm ERR! You can get their info via:
npm ERR! npm owner ls spawn-sync
npm ERR! There is likely additional logging output above.

npm ERR! System Linux 3.16.0-25-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /root/speesmane
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.4.21
npm ERR! code ELIFECYCLE
npm ERR! Error: ENOENT, lstat '/root/speesmane/node_modules/socketcluster/node_m odules/socketcluster-server/node_modules/uws/uws_darwin_48.node'
npm ERR! If you need help, you may report this entire log,
npm ERR! including the npm and node versions, at:
npm ERR! http://github.com/npm/npm/issues

How do I add additional player operation events to cell.js?

I'm trying to add functionality for players to place blocks and shoot bullets to iogrid. I thought I could just add more code to the game engine in cell.js, but apparently cell.js doesn't use sockets at all. I tried adding socket and socketCluster functionality to cell.js but I get this error Error: This socket has been ended by the other party.

If I'm understanding your code right, putting game engine stuff in worker.js will lead to massive slowdown. How should I go about adding these additional player operations if I can't do it in cell.js?

Can I use yarn install instead of npm install?

Currently if I use npm install it works OK. But with yarn install command I can't get through it because it says

Error: https://registry.yarnpkg.com/sc-cluster-broker-client: ETIMEDOUT
      at Timeout._onTimeout (/usr/local/lib/node_modules/yarn/node_modules/request/request.js:852:19)
      at ontimeout (timers.js:469:11)
      at tryOnTimeout (timers.js:304:5)
      at Timer.listOnTimeout (timers.js:264:5)

I really hope sc-cluster-broker-client can be installed via yarn because yarn is becoming more popular as a packaging library.

Error: listen EADDRINUSE :::15454

This is an error. Any idea what caused it when I ran the server.js using Cloud9.... I'm not sure what is going on!

Error: listen EADDRINUSE :::15454 at Object.exports._errnoException (util.js:907:11) at exports._exceptionWithHostPort (util.js:930:20) at Agent.Server._listen2 (net.js:1250:14) at listen (net.js:1286:10) at Agent.Server.listen (net.js:1382:5) at Object.start (_debug_agent.js:21:9) at startup (node.js:70:44) at node.js:974:3

Updating to latest socketcluster version

Is there any benefit to updating this example to the latest SocketCluster version (v14) or is it just a minor change that wouldn't help with performance. I attempted to migrate it, but it seems complex for a SC beginner like me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.