Icontem

comm.js: Send server requests via Websockets, AJAX or XDR

Recommend this page to a friend!
  Info   View files Documentation   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-01-09 (16 hours ago) RSS 2.0 feedNot enough user ratingsTotal: 209 This week: 4All time: 222 This week: 11Up
Version License JavaScript version Categories
comm 10MIT/X Consortium ...1.0AJAX, jQuery
Description Author

This is a jQuery plugin to send server requests via Websockets, AJAX or XDR.

It takes an object key-value pairs and sends a request to a server choosing the appropriate method according to the capabilities of the Web browser.

It uses XDomainRequest objects if the browser is Internet Explorer, Web sockets, or uses WebSockets if the URL protocol is ws or wss, or AJAX requests otherwise.

The plugin can retry sending the requests later if it was not possible to send it before.

Innovation Award
JavaScript Programming Innovation award winner
June 2014
Winner


Prize: One book of choice by Packt
Nowadays, there several ways to communicate with Web servers. Some ways are only available in certain versions of Web browsers.

This object provides a generic solution to send requests using either Websockets, AJAX requests or XDomainRequests.

Manuel Lemos
  Performance   Level  
Name: Jason Gerfen <contact>
Classes: 7 packages by
Country: United States United States
Innovation award
Innovation award
Nominee: 4x

Winner: 2x

Details

comm.js

Handles XHR, XDR, WS & WSS protocols

Fork me @ https://www.github.com/jas-/comm.js

Options

  • async: Force async opterational mode
  • binary: Force binary mode sends
  • url: If not specified the current page location is used
  • headers: A key/value object of headers to apply
  • method: The method to use (post, put, delete etc)
  • data: The data to be processed
  • timeout: Timeout value for retries
  • interval: Interval to use for retrying send upon connection termination

Examples

Here are a couple of examples

Default use

The default use case

comm(function(err, response){
	if (err) throw err;
	console.log(response);
});

A post example

An example sending a post request with a JSON object

  comm({
    method: 'post',
    data: {abc: '123', xyz: 345}
  }, function(err, res){
    if (err) throw err;
    console.log(res);
  });

Force ws/wss communications

Here is how you can use the websocket or secure web socket protocols

comm({
	data: 'ping test',
	url: 'ws://echo.websocket.org'
}, function(err, response){
	if (err) throw err;
	console.log(response);
});

Set customized headers

Need to use custom headers? See RFC-4229 for a complete list

comm({
	headers: {
		Content-Type: 'text/plain'
	}
}, function(err, response){
	if (err) throw err;
	console.log(response);
});

A note form submissions

To attach a form object as the payload the simpliest method would be the following example.

var formData = new FormData(document.getElementById('form-id'));
comm({
	data: formData
}, function(err, response){
	if (err) throw err;
	console.log(response);
});

A note on XDR

This is the least tested protocol this library supports. It will only be used when the clients browser is internet explorer, the version is less than 10 has access to the window.XDomainRequest object and if the URL specified does not match the current window.

A note on CORS

If you wish to use this for CORS requests which it does support you must configure your web server to allow the following header params (this example is tuned to support authentication credentials while limiting access vs. using a wildcard origin such as *)

Access-Control-Allow-Origin: https://example.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Cache-Control, Content-Type
Access-Control-Allow-Credentials: true
  Files folder image Files  
File Role Description
Accessible without login Plain text file bower.json Data Auxiliary data
Plain text file comm.js Class Class source
Plain text file comm.min.js Class Class source
Accessible without login Plain text file LICENSE Data Auxiliary data
Accessible without login Plain text file README.md Doc. Auxiliary data
Accessible without login Plain text file _config.yml Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:209
This week:4
All time:222
This week:11Up
User Comments (1)