Giter Club home page Giter Club logo

nengi's Introduction

nengi.js - multiplayer network engine

Hello friend. Nengi.js is a networking library/engine for node.js and HTML5.

Nengi straddles two types of performance: volume and responsiveness.

With respect to volume, it is possible to make a nengi game that can support 100+ concurrent players or 50,000+ entities on a 20 tick server.

Regarding responsiveness, the advanced api consists of tools that can eliminate input delay, setup lag compensated collisions, and smooth away lag. This boils down to a game with controls so responsive that it feels like single player.

Have your players saying:

"HOW IS THIS A BROWSER GAME?!"

Join us on nengi's Discord server or help support development on timetocode's Patreon

Currently compatible with node 14; will not work with node 15+

This is a stable branch of nengi, the very same that has a several millions of gameplays on it. It however relies on a websocket library that has no support past node 14 (cWS.js).

Node 16 support (NEW, also may not work with node 17)

There is an experimental branch called 'sixteen' which uses uWS.js for its websocket layer. The previously used library (cWS.js) was itself a fork of an earlier version of uWS.js. The nengi api remains unchanged EXCEPT: bots don't work (yet) and the SSL syntax has changed (see uWS.js docs for new ssl syntax). There's no particular reason to think that this branch of nengi is less stable, but it is being kept separate until more users have tested it and bot support has been added (which will have to come through an addtional websocket library, likely the one simply named "ws"). If you are using this branch please share your experiences on the nengi discord server. It has been noted by the creator of uWS.js that they will only support major node versions (14, 16, 18, etc) so the same can be said for this branch of nengi.

Features

  • Authoritative Server Model (anti-cheat)
  • Binary compression
  • Optimized game state snapshots
  • Networking culling
  • An easy to use api consisting of
    • Entities - persistent objects that stay synced in real time
    • Messages - events and other instantaneous occurrences
    • Commands - input from game clients
    • Channels - compose the above into more advanced features
  • Interpolation
  • Compatible with PIXI.js, Babylon.js, Three.js, and much more
  • Clientside prediction api
    • --> move instantly on the client
    • --> nengi will provide reconciliation data if needed
  • Lag compensated shots / collisions
    • --> a player fires a shot
    • --> nengi can rewind the game state to the point in time that the shot occured given the player's latency

Templates

Please note that these templates have not been updated recently, and while they should all work you may want to bump the nengi version to latest after checking them out.

There is a tutorial series for those learning nengi available at https://timetocode.com/nengi/intro-1. The code contained in these tutorials is pretty nice -- if you're making a 2D game in nengi without csp then forking this code makes for a great template.

Other templates:

  • nice-proto - A prototype of a simpler API, this is nengi-2d-csp under the hood. The most mature version of this api is in the tutorial mentioned above.
  • nengi-barebone - A barebone nengi template
  • nengi-2d-basic - A simple 2d shooter
  • nengi-2d-csp - A simple 2d shooter with prediction/compensation
  • nengi-babylon-3d-shooter - A template for 3D predicted games with Babylon.js
  • 3d-top-down - A top down game, in a 3d engine (Babylon.js)

Usage

The API documentation is the place to go for implementation details. But as an appetizer here is a tour of the the functionality associated with one of nengi's core features, the nengi.Entity

the nengi.Entity stack

// this is your own game object, it can have any properties or methods
class PlayerCharacter {
    constructor(x, y) {
        this.x = x
        this.y = y
        this.likesKittens = true
    }
    someLogic() {
        //etc
    }
}

// and this tells nengi what exactly to network about it
PlayerCharacter.protocol = {
    x: { type: nengi.Float32, interp: true },
    y: { type: nengi.Float32, interp: true }
}

The entity can then go on to be used in a nengi.Instance (the node.js game server)

const entity = new PlayerCharacter(50, 50)
instance.addEntity(entity)

Any changes that occur to the entity's networked properties (in this case x & y) will automatically be detected and synchronized. The nengi.Client (the HTML5 game client) will receive the following data which encompasses any state change on any entity:

const network = client.readNetwork()
network.entities.forEach(snapshot => {
    snapshot.createEntities.forEach(entity => {
        // entity { nid: 65534, ntype: 0, x: 50, y: 50, protocol: { name: 'PlayerCharacter', ... }} 
    })

    snapshot.updateEntities.forEach(update => {
        // update { nid: 65534, prop: 'x', value: 63, path: ['x'] }
    })

    snapshot.deleteEntities.forEach(nid => {
        // nid 65534
    })
})

This is everything the game client needs to keep any number of entities in sync: create, update, and delete. The data that traveled over the network was already optimized and highly compressed. The game state that comes out of client.readNetwork() is already smoothly interpolated and compatible with different frame rates (30, 60, 144, 250 etc).

This automatic synchornization of state on entities is the soul of nengi. It strikes a balance between configurability, performance, and ease of development. It also stays out of your game code. For things that are not easily represented as an entity, we have the nengi.Message, which can be used to manually network just about anything else.

The above is sufficient to network a vast variety of games where the game client sends commands to the game server and then waits on a reply from the server before the seeing the results of its commands. I refer to these as "non-predicted" games and every aspiring multiplayer programmer should make at least one before moving deeper with nengi.

That's the end of the basic path for a nengi entity, but the documentation covers the more advanced life of an entity. Summarized briefly, entities are culled, can be used for predictive movement on the game client, and have their state rewinded for advanced lag compensation.

nengi's People

Contributors

cfortuner avatar jarred-sumner avatar natethegreatt avatar shamblesides avatar timetocode avatar vickylance avatar

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

nengi's Issues

Features about scalable and room?

It's a great project!
I have a few questions.
can i make multiple processes by using nengi?
Is the project offer some features such as room making so that I can separate users into different rooms?

error

image get this error when trying to run negi

Latest stable 1.x version

Hi Alex,

Thanks for your great framework. This week I found out that the repository has moved to version 2.x. Unfortunately this version is not stable nor documented. What is the latest stable 1.x version I can safely use?

Many thanks

node version / module.link is not a function

What node version does nengi uses ?
On this page it says it runs with node 14, but when using it with Meteor (using Node 14) it throws this error:

/node_modules/nengi/index-es6.js:1
TypeError: module.link is not a function

Authentication example (with cookies)?

Before connecting to the game, players authenticate via passport.js outside the scope of Nengi.

To support that, I currently use this hack:

const wsServer = this.instance.wsServer;
wsServer.on("connection", (ws, req) => {
  var client = this.instance.connect(ws);

  client.onLoadSession = new Promise((resolve, reject) => {
    wsUse(session, req).then(() => {
      client.session = req.session;
      resolve();
    });
  });

  ws.on("message", (message) => {
    this.instance.onMessage(message, client);
  });

  ws.on("close", (event) => {
    this.instance.disconnect(client, event);
  });
});

This overrides what nengi.Instance does but adds this onLoadSession callback to run express-session and read their session cookie from the HTTP request

When the user is not authenticated, it disconnects them:

if (isGuest) {
  console.log("[GameInstance]", "Disconnecting guest");
  this.instance.disconnect(client, null);
  return;
}

Is there a better way of doing this? I'd prefer to use cookies instead of a JWT so that the client doesn't have to do anything special to auth requests

onConnect missing from types

I see this line (

//onConnect(callback: any): void
) has been commented out as none of these methods are intended for public consumption. Yet I see them being used in some of the examples (https://github.com/timetocode/nengi-babylon-3d-shooter/blob/master/server/niceInstanceExtension.js#L3 & https://github.com/timetocode/3d-top-down/blob/master/server/GameInstance.js#L17). I think I'm missing something here. Are these methods really meant to be commented out or is it being misused in the examples?

Also, I believe the onConnect type definition is missing from here https://github.com/timetocode/nengi/blob/master/index.d.ts#L133. Is it correct?

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.