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

freebird-transport

v0.6.0

Published

The transport for freebird REQ/RSP/IND message transmission

Downloads

9

Readme

freebird-transport

The transport for freebird REQ/RSP/IND message transmission

NPM

Travis branch npm PyPI npm

Table of Contents

  1. Overview
  2. APIs and Events
  3. TCP Server/Client Example

1. Overview

This module is the abstract class for developers to create their own transportation to communicate with freebird framework. This class inherits from EventEmitter which will emit a 'message' event when a message arrives.

  • In this document, transp will be used to denote the instance of this class.
  • In the following methods, the argument msg must be an object in the shape of { clientId, data }, where the data property is mandatory and should be a string or a buffer, and the clientId property is optional and should be a string or a number if given. One can attach an identifier, like a client id, to msg.clientId for message multiplexing.

For implementers:

To have a valid transp, two things must be done by the implementor.

  1. Must implement the transp._send(msg, callback) method on the transport instance.
  2. Must call the transp.receive(msg) method to let the transport instance know that there a message comes in. Calling transp.receive(msg) will induce a 'message' event triggered on the transport instance.

For users:

To use the transport object:

  1. Call transp.send(msg, callback) to send message out. It wil throw if _send() was not provided by the implementer.
  2. Listen to 'message' event to receive messages.

2. APIs and Events


new Transport()

Create the transport instance.

Arguments:

  1. none

Returns:

  • (Object): transp, instance of this class

Examples:

var Transport = require('freebird-transport');
var transp = new Transport();

._send(msg, callback)

The implmentation of sending out the message.

Arguments:

  1. msg (Object): The message to trasmit out is a string or a buffer attached to data property of this object.
  2. callback (Function): function (err, bytes) { ... }. This err-first callback should be called after message sent off. The argument bytes tells how many bytes were sent.

Returns:

  • none

Examples:

var transp = new Transport();

transp._send = function (msg, callback) {
    var bytes;

    if (typeof msg.data === 'string')
        msg.data = new Buffer(msg.data);

    if (!Buffer.isBuffer(msg.data))
        return setImmediate(callback, new TypeError('msg.data should be a string or a buffer'));

    bytes = msg.data.length;

    // ... implemention of message sending

    // Finally, call callback
    callback(null, bytes);
};

._broadcast(msg, callback)

The implmentation of broadcasting the message. This is optional, it will use transp.send(msg, callback) internally by default if implementation is not given. This broadcasting method is used to send an indication to multiple remote clients.

Arguments:

  1. msg (Object): The message to broadcast out is a string or a buffer attached to data property of this object.
  2. callback (Function): function (err, bytes) { ... }. This err-first callback should be called after message sent off. The argument bytes tells how many bytes were sent.

Returns:

  • none

Examples:

var transp = new Transport();

transp._broadcast = function (msg, callback) {
    var bytes;

    if (typeof msg.data === 'string')
        msg.data = new Buffer(msg.data);

    if (!Buffer.isBuffer(msg.data))
        return setImmediate(callback, new TypeError('msg.data should be a string or a buffer'));

    bytes = msg.data.length;

    // ... implemention of message broadcasting

    // Finally, call callback
    callback(null, bytes);
};

.send(msg, callback)

Call this method to send the message off.

Arguments:

  1. msg (Object): The message to trasmit out must be a string or a buffer which should be attached to data property of this object.
  2. callback (Function): function (err, bytes) { ... }. Get called after message sent off. The argument bytes tells how many bytes were sent.

Returns:

  • none

Examples:

var transp = new Transport();

// assume transp._send was implemented

// call transp.send() to send message
transp.send({ data: 'Hello World' }, function (err, bytes) {
    if (err)
        console.log(err);
    else
        console.log(bytes + ' bytes were sent')
});

.broadcast(msg, callback)

Call this method to broadcast the message.

Arguments:

  1. msg (Object): The message to trasmit out must be a string or a buffer which should be attached to data property of this object.
  2. callback (Function): function (err, bytes) { ... }. Get called after message sent off. The argument bytes tells how many bytes were sent.

Returns:

  • none

Examples:

var transp = new Transport();

transp.broadcast({ data: 'Hello World' }, function (err, bytes) {
    if (err)
        console.log(err);
    else
        console.log(bytes + ' bytes were sent')
});

.receive(msg[, callback])

The Implemntor should call this method to inform the message arrival.

Arguments:

  1. msg (Object): This must be an object with a data property that holds the received message.
  2. callback (Function): function (err) { ... }. Optional. Get called after message sent off.

Returns:

  • none

Examples:

var transp = new Transport();

// Implemntor calls transp.receive() when message arrives
foo.on('data', function (data) {
    transp.receive({ data: data });
});

.on('message', function (msg) {})

The user can listen to the 'message' event to receive the message.

Arguments of the listener

  1. msg (Object): This must be an object with a data property that holds the received message of a string or a buffer.

Examples:

var transp = new Transport();

transp.on('message', function (msg) {
    console.log(msg.data.toString());
});

.on('unhandledMessage', function (msg) {})

The user can listen to the 'unhandledMessage' event to receive the message that is not processed by the freebird.

Arguments of the listener

  1. msg (Object): This must be an object with a data property that holds the received message of a string or a buffer.

Examples:

var transp = new Transport();

transp.on('unhandledMessage', function (msg) {
    console.log('Message not processed by freebird: ' + msg.data.toString());
});

3. TCP Server/Client Example

3.1 TCP Server

var net = require('net'),
    Transport = require('freebird-transport');

var transp, client;

transp = new Transport();

server = net.createServer(function (c) {
    client = c;

    client.on('end', function () {
        client = null;
    });

    // Message received
    client.on('data', function (data) {
        transp.receive({ data: data });
    });
});

server.listen(4321, function () {
    console.log('TCP server starts');
});

// Implementaion of _send
transp._send = function (msg, callback) {
    var bytes;

    if (typeof msg !== 'object')
        return setImmediate(callback, new TypeError('msg should be an object'));

    if (typeof msg.data === 'string')
        msg.data = new Buffer(msg.data);

    if (!Buffer.isBuffer(msg.data))
        return setImmediate(callback, new TypeError('msg.data should be a string or a buffer'));

    bytes = msg.data.length;

    if (!client)
        return setImmediate(callback, new Error('No client connected'));

    client.write(msg.data);
    setImmediate(callback, null, bytes);
};

module.exports = transp;

3.2 TCP Client

var net = require('net'),
    Transport = require('freebird-transport');

var transp, client;

transp = new Transport();
client = net.createConnection(4321, 'localhost', function () {
    transp._send = function (msg, callback) {
        var bytes;

        if (typeof msg.data === 'string')
            msg.data = new Buffer(msg.data);

        if (!Buffer.isBuffer(msg.data))
            return setImmediate(callback, new TypeError('msg.data should be a string or a buffer'));

        bytes = msg.data.length;

        if (!client)
            return setImmediate(callback, new Error('No client created'));

        client.write(msg.data);
        setImmediate(callback, null, bytes);
    };
});

client.on('end', function () {
    client = null;
});

// Message received
client.on('data', function (data) {
    transp.receive({ data: data });
});

module.exports = transp;