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

megadata

v1.2.0

Published

Smart messaging for games

Downloads

12

Readme

Megadata

GitHub tag npm npm Gitter

Greenkeeper badge Build Status: Linux & macOS Build Status: Windows Coveralls branch

Megadata is a library you can use to serialize/deserialize network game data.

This library will help you deal with:

  1. Defining type IDs and type classes
  2. Definining the serialization/deserialization format to use
  3. Re-using messages object, by creating and maintaining a message pool
  4. Sharing message libraries between client (JavaScript) and server (Node.js)
  5. TypeScript type checks

Requirements

  • Node.js 8.0 or higher

Installation

npm install --save megadata

You will also need to make sure that the following configuration is set in your tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true
  }
}

Usage

Defining message types

shared/messages/index.ts

import megadata, { TypeDecorator } from 'megadata'

export enum TypeIds {
  Join,
  Leave
}

export const Type: TypeDecorator<TypeIds> = megadata(module)

We first create a list of message types as a const enum and list the messages we will send and receive in our game. We then generate a @Type decorator

shared/messages/types/Join.ts

import { Type, TypeIds } from '../'
import MessageType from 'megadata/classes/MessageType'
import Binary, { Uint32 } from 'megadata/classes/BinarySerializationFormat'

@Type(TypeIds.Join, Binary)
export default class Join extends MessageType {
  @Uint32
  public time: number
}

shared/messages/types/Leave.ts

import { Type, TypeIds } from '../'
import MessageType from 'megadata/classes/MessageType'
import Json from 'megadata/classes/JsonSerializationFormat'

@Type(TypeIds.Leave, Json)
export default class Leave extends MessageType {
  public time: number
}

Next, we define what our Join and Leave messages type will look like, and how we should serialize and deserialize it.

Megadata ships with two serialization formats:

  1. binary: deals only with messages containing numbers, but is blazing fast
  2. json: deals with any kind of data, but is slower

You may notice that the Binary serialization format requires additional annotations this is required to define (and optimized) the size and speed of serialization and deserialization.

Sending and receiving messages

server/classes/Player.ts

import Connection from '...'
import MessageEmitter from 'megadata/classes/MessageType'
import MessageType, { IMessageType, MessageTypeData } from 'megadata/classes/MessageType'

export class Player extends MessageEmitter {
  constructor(private connection: Connection) {
    connection.on('message', async (buffer: ArrayBuffer) => {
      const message = MessageType.parse(buffer)
      await this.emitAsync(message)
      message.release()
    })
  }

  public async function send<T extends MessageType>(type: IMessageType<T>, data: MessageTypeData<T>) {
    const message = type.create<T>(data)
    const buffer = message.pack()
    await this.connection.write(buffer)
    message.release()
  }
}

Messages are recycled from an object pool to reduce the impact of garbage collection; therefore, it is important to remember to release messages back into the object pool once you are done with them.

server/classes/Game.ts

import Player from './Player'

import Join from 'shared/messages/types/Join.ts'

export default class Game {
  // ...

  public addPlayer(player: Player) {
    player.on(Join, ({ time }) => console.log('Join time:', time))
    player.on(Leave, ({ time }) => console.log('Leave time:', time))
  }
}

Messages and events auto-loading

Running a Node.js server with auto-loading

Auto-loading uses require.context, which is a webpack-specific API. When using Megadata with auto-loading in Node.js, you will therefore need to load the mock provided by the library.

ts-node -r megadata/register index.ts

Setting types auto-loading

shared/messages/index.ts

import megadata, { TypeDecorator } from 'megadata'

const types = require.context('./types/')

export enum TypeIds {
  Join,
  [...]
}

export const Type: TypeDecorator<TypeIds> = megadata(module, types)

The following code will dynamically load type classes on demand from the shared/messages/types folder. If no listeners were ever set to listen for messages of this type, an Event.Unknown event will be emitted (see below).

Setting event handlers auto-loading for a class inheriting MessageEmitter

server/classes/Player.ts

import MessageEmitter, { AutoloadEvents } from 'megadata/classes/MessageEmitter'

const events = require.context('../events/')

@AutoloadEvents(events)
export default class Player extends MessageEmitter {}

A given message emitter may end up handling a large number or events. Event handlers auto-loading provides a mechanism for breaking event handling down into event handler files that are auto-loaded on demand. In this case, we will auto-load all events under server/events.

server/events/Join.ts

import Player from '../classes/Player'
import Join from 'shared/messages/types/Join'

export default function (player: Player) {
  player.on(Join, (message) => console.log('Received join event', message))
}

Event handler files export a single default function which will receive a message emitter instance; you may then set the even listeners according to your needs.

Custom serialization

shared/messages/classes/CustomSerializationFormat.ts

import MessageType from './MessageType'
import SerializationFormat, { ISerializerFunctions } from './SerializationFormat'

export default class CustomSerializationFormat extends SerializationFormat {
  public create<I, T extends MessageType>(id: I, size: number, attributes: any) {
    return {
      create: (...),
      pack: (...),
      unpack: (...),
    } as ISerializerFunctions<I, T>
  }
}

You can create your own custom serialization format. Above is a quick stub, but you should have a look at the default serialization formats provided by megadata.

Acknowledgements

  • Initial code and implementation (JavaScript): @ronkorving
  • Logo customizations: @lzubiaur

License

MIT License. Copyright (c) Wizcorp Inc.