Created
August 27, 2010 12:57
-
-
Save rwaldron/553303 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// close stream: label-a | |
$.eventsource('close', 'label-a'); | |
// close stream: label-b | |
$.eventsource('close', 'label-b'); | |
// close all open streams | |
$.eventsource('close'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Returns an object containing all the currently active eventsource streams | |
$.eventsource('streams') | |
var stream = $.eventsource('streams', 'label'); | |
stream.history : Object containing the event message history for specified stream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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) { | |
// do something when the stream opens | |
}, | |
// Set a callback to fire when a message is received | |
// `onmessage` | |
message: function (data) { | |
// Do something with the event source response | |
console.log(data); | |
} | |
}); | |
// Close event sources by label name | |
$.eventsource('close', 'event-source-label'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PLAIN TEXT EXAMPLE - NO CONTENT TYPE GIVEN | |
$.eventsource({ | |
label: 'text-event-source', | |
url: 'test-event-sources/text-event-source.php', | |
open: function () { | |
// do something when the stream opens | |
}, | |
message: function (data) { | |
// `data` will default to type/text | |
console.log(data); | |
} | |
}); | |
// PLAIN TEXT EXAMPLE - HAS CONTENT TYPE | |
$.eventsource({ | |
label: 'text-event-source-ct', | |
url: 'test-event-sources/text-event-source-ct.php', | |
dataType: 'text', | |
open: function () { | |
// do something when the stream opens | |
}, | |
message: function (data) { | |
// `data` is explicitly type/text | |
console.log(data); | |
} | |
}); | |
// JSON EXAMPLE - HAS CONTENT TYPE | |
$.eventsource({ | |
label: 'json-event-source', | |
url: 'test-event-sources/json-event-source.php', | |
dataType: 'json', | |
open: function () { | |
// do something when the stream opens | |
}, | |
message: function (data) { | |
// `data` is explicitly type/json | |
console.log(data); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment