Code Monkey home page Code Monkey logo

jquery-graceful-websocket's Introduction

The Graceful WebSocket

So, you want to start building realtime event driven applications using the new HTML5 WebSocket API?

  • You want it to work in all browsers, no matter if they have WebSocket support or not
  • You don't want to rely on proprietary technology such as Flash to provide a fallback
  • And naturally, you don't want to write more than one implementation

Introducing, the gracefulWebSocket jQuery plugin:

  • Implements the w3c WebSocket interface
  • Wraps the native WebSocket if support is detected
  • Provides a default fallback using traditional AJAX polling over HTTP
  • Requires no extra code on the frontend, allows you to target the WebSocket API today and let users take advantage of it as more browsers add support.
  • Default fallback behavior can be overridden by plugin options

Contact: http://twitter.com/ffdead

Basic Usage

Basic usage is very straight forward. The whole purpose of this plugin is to enable developers to use the WebSocket API to write applications, even if the browser doesn't support it.

Include plugin

// include the plugin
<script type="text/javascript" src="jquery.gracefulWebSocket.js"></script>

Open WebSocket

// open socket using standard syntax
var ws = $.gracefulWebSocket("ws://127.0.0.1:8080/");

Sending

// send message to server using standard syntax
ws.send("message to server");

Receiving

// listen for messages from server using standard syntax
ws.onmessage = function (event) {
   var messageFromServer = event.data;   
};

Graceful degradation explained

The gracefulWebSocket will automatically revert to fallback mode if no native WebSocket implementation can be detected in the browser.

A fallback object implementing the same interface with be returned instead, which uses HTTP POST to send messages to the server, and polling HTTP GET requests to retrieve new messages from the server.

// in fallback mode: connect returns a dummy object implementing the WebSocket interface
var ws = $.gracefulWebSocket("ws://127.0.0.1:8080/"); // the ws-protocol will automatically be changed to http
// in fallback mode: sends message to server using HTTP POST
ws.send("same message to server, this time send using a POST request");
// in fallback mode: listens for messages by polling the server using HTTP GET
ws.onmessage = function (event) {
   var sameMessageFromServer = event.data;   
};

More info

http://www.slideshare.net/ffdead/the-html5-websocket-api

MIT License

Copyright (c) 2010 David Lindkvist

jquery-graceful-websocket's People

Contributors

andreif avatar

Watchers

 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.