Channel Uniform
API for performing http(s) request and opening websockets uniformly across node.js and browsers.
- Use
require("channel-uniform/node")
inside node applications. - Use
require("channel-uniform/browser")
inside modules to be browserified.
// Uncomment one of the two lines below:// var Channel = require("channel-uniform/node");// var Channel = require("channel-uniform/browser"); // requestvar channel1 = ;var response = channel1;console;channel1; // websocketvar channel2 = var ws = channel2;ws { ws { console; }; ws;};
http-sync
{ status: 200,
reason: 'OK',
headers:
{ server: 'meinheld/0.6.1',
date: 'Thu, 18 May 2017 13:30:07 GMT',
'content-type': 'application/json',
'access-control-allow-origin': '*',
'access-control-allow-credentials': 'true',
'x-powered-by': 'Flask',
'x-processed-time': '0.000710010528564',
'content-length': '32',
via: '1.1 vegur' },
body: '{\n "origin": "81.164.22.111"\n}\n' }
http-async
{ status: 200,
reason: 'OK',
headers:
{ connection: 'close',
server: 'meinheld/0.6.1',
date: 'Thu, 18 May 2017 13:30:08 GMT',
'content-type': 'application/json',
'access-control-allow-origin': '*',
'access-control-allow-credentials': 'true',
'x-powered-by': 'Flask',
'x-processed-time': '0.000776052474976',
'content-length': '32',
via: '1.1 vegur' },
body: '{\n "origin": "81.164.22.111"\n}\n' }
ws
Hello!
channel = Channel(host, secure)
Instantiate a new channel.
-
host(string)
: defines the other end point of the channel, understand the formats detailed below.Format Example Remark host and port "www.example.org:80"
host only "www.example.org"
A default port is used: 80 for plain connections and 443 for encrypted connection port only "8080"
Local port, equivalent to "localhost:8080"
path "/path/to/unix-socket" Absolute path to a unix-domain-socket, faster but works only on node -
secure(boolean)
: indicates whether the communication should be encrypted or not. -
channel(object)
: instance of this module.
response = channel.request(method, path, headers, body)
Perform a synchronous http(s) request, may throw an error.
channel(object)
: instance of this module.method(string)
: http method (eg:"GET"
or"POST"
).path(string)
: path of the http(s) requestheaders(object)
: mapping from header keys to header valuesbody(string)
: body of the http(s) request.response(object)
: buffered response objectresponse.status(number)
: http status code (eg:200
or404
).response.reason(string)
: reason stated in the status lineresponse.headers(object)
: mapping from header keys to header valuesresponse.body(string)
: body of the response
channel.request(method, path, headers, body, callback)
Perform an asynchronous http(s) request.
callback(function|any)
:- If callback is a function it will be called once the request is completed with two arguments:
- an instance of
Error
if it failed andnull
if it succeed - a response object similar as with synchronous requests
- an instance of
- Else if callback is a truthy value, the response will not be parsed (faster).
- Else a synchronous request will actually be performed.
- If callback is a function it will be called once the request is completed with two arguments:
Attention: On node, when the callback parameter is a function, the request will only be fired only after resuming to the event loop which is the expected behavior of http.request. Every other communication in this module are fired immediately.
websocket = channel.websocket(path)
path(string)
: path for the http(s) upgrade request.websocket(object)
: instance ofwindow.WebSocket
in browsers and an instance of ws in node, both provide the same basic API:var websocket = channel;websocket { ... };websocket { ... };websocket { ... };websocket {var message = eventdata;...};// once open //websocket;websocket;