• Stars
    star
    186
  • Rank 200,136 (Top 5 %)
  • Language
    JavaScript
  • Created almost 14 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Harnessing the EventSource API with jQuery

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"

More Repositories

1

idiomatic.js

Principles of Writing Consistent, Idiomatic JavaScript
23,134
star
2

johnny-five

JavaScript Robotics and IoT programming framework, developed at Bocoup.
JavaScript
13,201
star
3

tc39-notes

TC39 Meeting Notes
JavaScript
587
star
4

jquery-hive

jQuery Plugin for creating and managing web workers across implementations. Includes Hive.Pollen.js - the thread-safe utility library for connecting worker threads to the Hive.
JavaScript
203
star
5

particle-io

Particle/Spark Core/Photon IO Plugin for Johnny-Five
JavaScript
172
star
6

io-plugins

Documentation and discussion point for IO Plugins
118
star
7

dmv

JavaScript
118
star
8

galileo-io

Intel Galileo & Intel Edison IO Plugin for Johnny-Five
JavaScript
101
star
9

temporal

Non-blocking, temporal task sequencing. For use with robots built with Johnny-Five
JavaScript
84
star
10

javascript-robotics

JavaScript
71
star
11

grunt-compare-size

JavaScript
62
star
12

proposal-math-extensions

ES Math Extensions
HTML
58
star
13

popcorn.capture

capture any frame from your popcorn video instance and save as png
JavaScript
57
star
14

tessel-io

A Tessel 2 IO Plugin for Johnny-Five JavaScript Robotics programs
JavaScript
50
star
15

proposal-enum-definitions

47
star
16

fritzing-components

Custom Fritzing parts
40
star
17

fact

JavaScript
38
star
18

popcorn.sequence

JavaScript
36
star
19

mediagroup.js

JavaScript
28
star
20

etherport

EtherPort is a transport layer
JavaScript
23
star
21

navigator.getusermedia

JavaScript
22
star
22

popcorn.kernel

JavaScript
20
star
23

playground-io

Adafruit Circuit Playground IO Plugin for Johnny-Five
JavaScript
15
star
24

nodebot-sbc

NodeBots SBC
14
star
25

rwaldron

JavaScript
14
star
26

esp-io

ESP-IO is an IO Plugin that enables writing JavaScript Robotics programs with Johnny-Five, which interact with ESP8266 boards.
13
star
27

imp-io

Electric Imp IO Plugin for Johnny-Five
JavaScript
12
star
28

arduino-adc-io-expander

Turn any Arduino into an ADC IO Expander with an I2C interface.
Arduino
12
star
29

mock-firmata

Mocks used for testing code that depends on Firmata
JavaScript
11
star
30

compulsive

loops and delays without traditional timers
JavaScript
11
star
31

bro

whats up bro
10
star
32

jquery-mobile-badge

JavaScript
10
star
33

subjectisdead

Haskell
9
star
34

popcorn.embedly

JavaScript
9
star
35

edison-io

Intel Edison & Intel Galileo IO Plugin for Johnny-Five
JavaScript
9
star
36

j5-ds-touch

A DS Touch Screen component plugin for Johnny-Five
JavaScript
9
star
37

idiomatic.json

8
star
38

tyrion

Agent/Device Firmware Pair for Electric Imp. Use with Imp-IO for Johnny-Five
Squirrel
8
star
39

butter

JavaScript
8
star
40

unprefix.js

JavaScript
8
star
41

j5-rc-receiver

RC Receiver component plugin for Johnny-Five
JavaScript
8
star
42

chromakey

html5 video chromakey
JavaScript
8
star
43

nino-io

Linino One & Arduino Yun IO Plugin for Johnny-Five
JavaScript
8
star
44

adventure

JavaScript
7
star
45

burst-core

First pass at re-writing the animation core of The Burst Engine for generic JS Object timeline control.
JavaScript
6
star
46

pee-wee-rover

JSConf 2015 JavaScript Robotics Project Files
JavaScript
5
star
47

node-jsdev

JavaScript
5
star
48

jQuery.Mines

Publisher/Subscriber system with expressive and flexible control - and a cool metaphor.
JavaScript
5
star
49

tiny-query

JavaScript
5
star
50

tinyquery

JavaScript
4
star
51

EventSource

THIS IS NOT A RELEASE
JavaScript
4
star
52

polyfill

JavaScript polyfill functions
JavaScript
4
star
53

avrdude

Arduino-specific version of avrdude
C
3
star
54

jquery-classlist

JavaScript
3
star
55

snippets.js

tab triggered js code snippets... everywhere!
JavaScript
3
star
56

i2c-backpack-workshop

3
star
57

sdactivity

JavaScript
3
star
58

sensors

Notes for Sensor API unification
3
star
59

espresso

Small JavaScript VM for Arduino Mega 2560
C
3
star
60

platoon

JavaScript
3
star
61

footils

For exercisin distributed development
JavaScript
3
star
62

nitwit

nitwit is a command line twitter status update program in python
3
star
63

reflecta-io

IO Plugin for controlling Arduino boards with Johnny-Five via Reflecta
JavaScript
3
star
64

popcorn.zoom

Popcorn plugin to zoom and rotate popcorn video objects
JavaScript
3
star
65

vox-populi-tests

2
star
66

burst

A javascript vector animation engine for the HTML5 Canvas supporting SVGs, Blender3D objects, timelines, easing, command chanining and callbacks.
JavaScript
2
star
67

NXTI2CDevice

Patched version of NXTI2CDevice for use with Arduino 1.0+
C++
2
star
68

sotu

JavaScript
2
star
69

popcorn.create

JavaScript
2
star
70

pcduino-io

pcDuino IO Plugin for Johnny-Five
JavaScript
2
star
71

electron-io

Particle Electron IO Plugin for Johnny-Five
JavaScript
2
star
72

j5-button-gestures

JavaScript
2
star
73

lemmy

49% Motherfucker | 51% Son of a Bitch Express + Mocha + HTML5 smart boilerplate for both Javascript and Coffee-Script that kicks your ass!!!
CoffeeScript
2
star
74

video-js

Universal video embed.
JavaScript
2
star
75

testharness.js

Test framework for writing javascript-based browser tests
JavaScript
1
star
76

b9snvs0

1
star
77

cloudnine

hooking up with cloud9 beta
JavaScript
1
star
78

nodysentary.js

1
star
79

taco

It's a tastier docco built with bootstrap.
CoffeeScript
1
star
80

blitz-node

Node.js API Client for Blitz
JavaScript
1
star
81

Promise

An elegant promise implementation for Javascript
JavaScript
1
star
82

tubeyloops

JavaScript
1
star
83

cloudninetest

cloudninetest
JavaScript
1
star
84

popcorn.scene

JavaScript
1
star
85

t2-project

Build a project for deployment to a Tessel 2
JavaScript
1
star
86

animeiosis

JavaScript
1
star
87

junktown

1
star
88

tessel-include-bundling

JavaScript
1
star
89

sumobot

SumoBot class for Johnny-Five
JavaScript
1
star
90

duokey

JavaScript
1
star
91

junkbox

1
star
92

geniverse

JavaScript
1
star
93

chrome-dev-snippets

JavaScript
1
star
94

ObjectLateral

Passing off objects since jqcon 2009
1
star
95

webOS

webOS Related Projects.
JavaScript
1
star
96

jquery-pollute

jQuery.Pollute() is a plugin designed to make polluting the global scope with variables and functions VERY EASY! Forget about closures and scoping, now all your REALLY USEFUL, ULTRA SPECIFIC code is available anywhere in your program!!
JavaScript
1
star
97

scripty2-contrib

1
star
98

euler

JavaScript
1
star
99

tiny-jquery

Interview project
JavaScript
1
star
100

playback.js

JavaScript
1
star