npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

net-peer

0.4.0 • Public • Published

net-peer

Build Status

websockets are based on TCP. They aren't viable for performant real-time games and simulations.

UDP or WebRTC are more appropriate, because these networking layers expose an interface with very low latency (albeit lossy,) packet oriented data transmission.

net-peer provides a light layer on top of this layer, offering:

  • unreliable & reliable ordered transports
  • chunk transport
  • automatic packet loss detection and retransmission
  • crc packet integrity checking
  • tests
  • small ish codebase
  • few external dependencies
  • works in the browser (webRTC) and node (UDP)

usage

This module assumes the underlying connection is using simple-peer, but it can be adapted to use other transports. See examples/udp/ to see a working example of this module used with node's UDP sockets.

const netPeer = require('net-peer')
 
// ... various events to set up webrtc and get a new peer connection
 
// peer is a simple-peer instance. Represents
// the connection to another WebRTC peer.
const p = netPeer(peer)
 
function frame() {
  p.step()
  setTimeout(frame, 0)
}
 
frame()
 
// send a 5 byte, unreliable message to the peer
p.sendUnreliable(new Uint8Array([ 24, 3, 9, 12, 254 ]))

API

getLatency

get the total round-trip latency of the connection in milliseconds. Latency is calculated as an exponentially smoothed average over time.

const latency = p.getLatency()

sendChunk

when you need to send a large chunk of data quickly and reliably, and is not immediately time critical (for example, sending data for a game's level) a chunk is a good way to do this:

const levelToLoad = new Uint8Array([ ... ])
 
p.sendChunk(levelToLoad)

receiving data

net-peer instances publish data events, which you can use to receive data.

p.subscribe('data', function handleMessage(peer, data) {
  // data is a Uint8Array contianing the received message (chunk, reliable/unreliable packets)
})

sendReliable

You occasionally need data that is reliably delivered, and never dropped (e.g., chat messages, critical game commands, etc.) sendReliable will send reliable, ordered messages for these kinds of situations.

const crucialGameMessage = new Uint8Array([ ... ])
p.sendReliable(crucialGameMessage)

sendUnreliable

In real-time games and other simulations, the bulk of the data sent over the network is useful for a very limited period of time (think character position, rotation, etc.) This data should be sent in a way that minimizes latency, at the cost of being less reliable. Use this transport method for sending data which might be lost, but is extremely low latency and preserves the message order.

// this data is sent very frequently and changes often
const characterData = new Uint8Array([ posx, posy, rotation, hp, ... ])
 
p.sendUnreliable(characterData)

credit

heavily inspired and guided by Gaffer's networking articles on building a game networking protocol

http://gafferongames.com/building-a-game-network-protocol/

Keywords

none

install

npm i net-peer

Downloadsweekly downloads

28

version

0.4.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability