Skip to content

Transport

Eugene Lazutkin edited this page May 24, 2017 · 2 revisions

Transports are I/O request handlers, which may use different physical means to perform their tasks. io uses an XHR as a default transport, which handles HTTP requests. An alternative fetch-based transport can be used as well.

Additionally io comes bundled with jsonp and load custom transports.

The current default transport is saved in io.defaultTransport. Custom transports are registered in io.transports, and represented by a single function (compare with Service callback function). See the main API for more details.

A custom transport to use is indicated by transport property of options object.

Transport callback

A transport callback is a function, which takes two parameters:

  • options — an options object described in the main API. It describes an I/O request.
  • prep — a helper object made with io.prepareRequest() (see the main API).

It should return a promise.

Example

Let's construct a fake custom transport, which always returns a constant:

define(['heya-io/io'], function (io) {
  'use strict';

  function transportHandler (options, prep) {
    return io.Deferred.resolve(42);
  }

  io.transports.fake = transportHandler;

  return io.makeVerb('fake', 'transport');
});

This is how we use it:

io.fake('/DeepThought', {question: 'the Ultimate One'}).
  then(function (result) {
    console.log(
      'The Answer to the Ultimate Question of Life, ' +
      'The Universe, and Everything is ', result);
  });

Clone this wiki locally