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

engine.io-binary.events

v1.0.2

Published

Adds an event system to be used with engine.io server/client while sending binary data.

Downloads

3

Readme

engine.io-binary.events

engine.io-binary.events adds an event system to be used with the engine.io server/client library while sending binary data.

Installing

npm install engine.io-binary.events

To run client-side, copy the binary.events.js file from /lib

How To Use

Create an Event list

These events must be shared by both the client and server!

//events to be sent server-side
var server_events = [
    'server_update_position',
    'server_object_hit'
];
//events to be sent client-side
var client_events = [
    'client_move',
    'client_jump'
];

Server - Sending/Receiving

var engine = require('engine.io');
var BinaryEvents = require('engine.io-binary.events')(server_events, client_events);
var server = engine.listen(80);

server.on('connection', function(socket){
    //if only receiving binary data
    socket.on('message', BinaryEvents.onBinaryEvent);
    //or if you need to check for other types of data
    socket.on('message', function(data){
        if(data instanceof Buffer){
            BinaryEvents.onBinaryEvent.call(socket, data);
        }
    });

    //register your events in defined client_events
    socket.on('client_jump', function(buf){
        //the buffer will still contain the index of the client event in the first byte
        //buf.readUInt8(0) = index of client_jump in client_events(1 in this case)
    });
    socket.on('client_move', function(buf){});

    //creates a buffer with a length of 4 bytes(excluding the event index byte)
    var buf = BinaryEvents.createBuffer('server_update_position', 4);
    //if using default options the first byte of the buffer will contain the event index
    //make sure not to overwrite this
    //add some data
    buf.writeUInt16LE(1, 100);
    buf.writeUInt16LE(3, 300);
    socket.send(buf);
});

Client - Sending/Receiving

<script src="/path/to/engine.io.js"></script>
<script src="/path/to/binary.events.js"></script>
<script>
  // eio = Socket
  var socket = eio('ws://localhost');
  var BinaryEvents = eioBE(client_events, server_events);
  socket.on('open', function(){
    socket.on('message', BinaryEvents.onBinaryEvent);
    socket.on('server_update_position', function(buf){
        //the first byte will contain the event index
        var dv = new DataView(buf);
        dv.getUint16(1);
        dv.getUint16(3);
    });
    //send ArrayBuffer with client_jump event
    var buf = BinaryEvents.createBuffer('client_jump', 1);
    var dv = new DataView(buf);
    dv.setUint8(1, 40);
    socket.send(buf);
  });
</script>

API

Top Level

Node.js

require('engine.io-binary.events') returns a BinaryEventsManagerconstructor(see below).

Browser/Client Side

window.eioBE is a BinaryEventsManager constructor(see below).

BinaryEventsManager

Constructor
  • (send_events, receive_events [, options])
    • Returns a new BinaryEventsManager instance.
    • Parameters
      • send_events: an array of strings. These are the event names you'll be sending.
      • receive_events: an array of strings. These are the event names you'll be receiving.
      • options: optional, options object.
      • Options
        • event_byte_len: Defaults to 1. The amount of bytes the event will use. If you need more than 256 events use 2 bytes.
        • litte_endian: Defaults to false. If true the event index is stored and read as little endian.

Properties

  • options: returns a read only object.

Methods

  • createBuffer( event_name, length )
    • Returns: ArrayBuffer(client) or Buffer(Node.js)
    • Parameters
      • event_name: One of the events in send_events.
      • length: The amount of bytes the buffer will have.
  • onBinaryEvent(data)
    • Fires any event attached to the Socket with a matching receiving event index.
    • Context: must have Socket as its calling object. See examples above.
    • Parameters
      • data: any data received from the Socket message event.

Testing

Tests can be run with Node.js using npm test

There is also a server-client test in tests/server. Start the server with node server and then open server_client_test.html with a browser.

How It Works

enigine.io-binary.events works by adding an identifier – in this case a number – to the beginning of any binary data you send. This identifier will be the index of an event name in one of the arrays you provide when loading the library.

When the data is received, the identifier is stripped and the index is matched to an event name - that event is then fired.

As the event names are referenced on both the client and server-side, it is of most importance that the arrays of these event names are identical.