Coder Social home page Coder Social logo

jquery.eventsource's Introduction

jQuery.EventSource

2011-05-30 Update: Tests passing 100% in Firefox/Aurora 6

TODO: Rewrite with jQuery Deferred Objects

Gives developers the power of the EventSource API across browsers. Uses the EventSource constructor when natively available and falls back to Ajax polling logic when it's not. Contributions must conform to Idiomatic Style Manifesto

http://wiki.github.com/rwldrn/jquery.eventsource/

http://code.bocoup.com/jquery.eventsource/unit-tests/

Some Thoughts...

  • Q. why use EventSource rather than Websockets?
    A. EventSource is easier to setup on server (uses http), lighter and most of the times - that's what you acually need. (via temp01)

Real World Example

http://yakyakface.com/

Provides cross browser implementation for:

http://dev.w3.org/html5/eventsource/

Unit tests

http://yakyakface.com/jquery.eventsource/unit-tests/

For a libray agnostic polyfill that offers base implementation fallback, check out Remy Sharp's work here: pollyfills/EventSource.js

Usage

	$.eventsource({
		
		// Assign a label to this event source
		
		label: "event-source-label", 

		// Set the file to receive data from the server

		url: "event-sources/server-event-source.php",
		
		// Set the type of data you expect to be returned
		// text, json supported
		
		dataType: "json", 
		
		// Set a callback to fire when the event source is opened
		// `onopen`
		open: function( data ) {


			console.log( data );

		},

		// Set a callback to fire when a message is received
		// `onmessage`
		message: function( data ) {


			console.log( data );

		}
	});
	
	
	// Close event sources by label name
	
	$.eventsource("close", "event-source-label");

Varied Content Type Usage

	// PLAIN TEXT EXAMPLE - NO CONTENT TYPE GIVEN
	$.eventsource({
		label: "text-event-source",
		url: "test-event-sources/text-event-source.php",
		open: function() {

			console.log( "opened" );

		},
		message: function( data ) {

			console.log( data );


			$.eventsource("close", "text-event-source");
		}
	});
	
	// PLAIN TEXT EXAMPLE - HAS CONTENT TYPE
	$.eventsource({
		label: "text-event-source-ct",
		url: "test-event-sources/text-event-source-ct.php",
		dataType: "text",

		message: function( data ) {

			console.log( data );

			$.eventsource("close", "text-event-source-ct");
		}
	});


	// JSON EXAMPLE - HAS CONTENT TYPE
	$.eventsource({
		label: "json-event-source",
		url: "test-event-sources/json-event-source.php",
		dataType: "json",
		open: function() {

			console.log( "opened" );
	
		},
		message: function( data ) {

			console.log( data );

			$.eventsource("close", "json-event-source");
		}
	});		 

Accessing your current event sources:

	// Returns an object containing all the currently active eventsource streams
	$.eventsource("streams")

Server Source Requirements

	// Server response MUST be Content-Type: text/event-stream
	// Server response MUST be prepended with "data: "

	
	// Examples:
	
	// PHP
	header("Content-Type: text/event-stream\n\n");
	echo "data: this is a valid response";
	
	
	// Python
	print "Content-Type: text/event-stream"
	print "data: this is a valid response"

jquery.eventsource's People

Contributors

rwaldron avatar

Watchers

abrupte 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.