npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

netcode

v2.1.1

Published

NetCode for web video games

Downloads

17

Readme

Netcode

A simple JavaScript client & server binary-encoded websocket communication system aimed towards web video games development.

Features:

  • 🔌 Server / Client duo, for node and the browser, that handle Websocket connection and communication.
  • ⚡️ Handle the binary encoding and decoding of your data, with performances in mind.
  • 📢 Listen for event dispatched over websocket with simple on/off event emitter system.
  • 💬 Fallback to JSON for easy debugging.

Requirements

  • Node >= v8.0.0

Installation

npm add netcode

Get started

Define a list of events

The server and the client must share the same events. An event is defined by its unique name and the corresponding codec, responsible for encoding and decoding the data.

// events.js
import Int8Codec from 'netcode/src/encoder/codec/Int8Codec';
import StringCodec from 'netcode/src/encoder/codec/StringCodec';

export default [
	['id', new Int8Codec()],
	['say', new StringCodec()],
];

In this example, the event list define how to send the following events over websocket:

  • client.send('id', 255);
  • client.send('say', 'Hello world!');

Then you'll be able to listen to this events on the client as follow:

  • client.on('id', id => { /* Do something */ });
  • client.on('say', sentence => { /* Do something */ });

Now let's create a server and an client that use this event list.

Setup a Server

We setup a server specifying the port and host on which the server will listen and the type of encoder to use. Here we use a BinaryEncoder to communicate in binary over websocket, with the previously configured event list.

import Server from 'netcode/src/server/Server';
import BinaryEncoder from 'netcode/src/encoder/BinaryEncoder';
import events from './events';

// Listen on localhost:8080
const server = new Server(8080, 'localhost', new BinaryEncoder(events));

server.on('client:join', client => {
	client.on('say', sentence => console.log(sentence));
	client.send('id', client.id);
});

Now we've got a server running at localhost:8080 that listen for a say text event and send a id integer event to every client that connects.

See an full example of server setup.

Write a Client

Now we write a client, for the browser, that connects to our running server on ws://localhost:8080 and use a BinaryEncoder with the same event list as the server.

import Client from 'netcode/src/client/Client';
import BinaryEncoder from 'netcode/src/encoder/BinaryEncoder';
import events from './events';

const client = new Client('ws://localhost:8080', new BinaryEncoder(events))

client.on('open', () => {
	client.on('id', id => console.log(`My id is ${id}.`));
	client.send('say', 'Hello world!');
});

Now we've got client that listen for the id event and sent a sentence in a say event.

Connection is alive and well!

See an full example of client setup.

Complete documentation

To go further, see in-depth documentation and how-to's.