Coder Social home page Coder Social logo

transport's Introduction

#Transport

A hub for centralizing all your request handlers within your application. Write handlers for streaming files, doing http requests, querying databases, REST apis, other applications or anything else and hide everything behind a convenient and straightforward API. The handlers can be shared between several Transport, like the SocketIOTransport for instance, which will hide socket.io behind the same interface so you can have your handlers on the server side and query them from the client side!

#Installation

npm install transport

#How to use

Require and initialize transport:

var Transport = require("transport"),
  transport = new Transport();

Requests

Define a simpleHandler to handle a request and send the result back:

// Object with all the request handlers
var handlers = {
    simpleHandler: function (payload, callback) {
        // do something with the payload
        var result = doSomethingWithPayload(payload);

        // send the result back
        callback(result);
    }
};

Add the handler to Transport. The handlers have to be stored in an observable-store. This allows for sharing the same handlers between several implementations of Transport and react accordingly when handlers are added and removed.

var Store = require("observable-store");

transport.setReqHandlers(new Store(handlers));

Make the request to simpleHandler:

transport.request("simpleHandler", "payload", function callback(result) {
    // do something with result
});

Open a channel

Let's define a handler that will publish several updates.

var handlers = {
    simpleChannel: function (payload, onEnd, onData) {
        var stream = getStream(payload);

        stream.on('data', onData);
        stream.on('end', onEnd);
    }
};

Now we can open the channel and receive updates:

transport.listen("simpleChannel", "filename", function onEnd(data) {
    // Will be called when stream closes
    console.log("END OF FILE", data);
}, function onData(data) {
    // Will be called everytime some data is pushed
    console.log("MORE DATA", data);
});

Close/dispose an open channel

transport.listen returns a function. When the handler also returns a function, it will be called by executing the one returned by transport.listen. It can be used to stop whatever the handler started and do some cleanup too.

var handlers = {
    closableChannel: function (payload, onEnd, onData) {
        var stream = getStream(payload);

        stream.on("data", onData);
        stream.on("end", onEnd);

        return function stop() {
            stream.removeListener("data", onData);
            stream.removeListener("end", onEnd);
        }
    }
};

Now, when calling transport.listen, we can call the stop() function returned by the handler.

var stop = transport.listen("closableChannel", "filename", function onEnd() { ... }, function onData() { ... });

// When calling stop, the stop() function returned by the handler will be executed.
stop();

Managing errors

A convenient way to bubble up errors is to follow the error first convention:

var handlers = {
    closableChannel: function (payload, onEnd, onData) {
        var stream = getStream(payload);

        stream.on("data", onData);
        stream.on("end", function (data) {
            onEnd(null, data);
        });
        stream.on("error", function (error) {
            onEnd(error);
        });
    }
};

And now we can handle the errors in the onEnd callback.

transport.listen("closableChannel", "filename", function onEnd(error, data) {
    if (error) {
        throw new Error(error);
    }

    // do something with data
}, function onData() {
        // ...
});

LICENSE

MIT

transport's People

Contributors

podefr avatar

Stargazers

Olivier Wietrich avatar

Watchers

 avatar James Cloos avatar Olivier Wietrich avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.