Skip to content

Instantly share code, notes, and snippets.

@edstenson
Created September 23, 2011 17:06
Show Gist options
  • Select an option

  • Save edstenson/1237903 to your computer and use it in GitHub Desktop.

Select an option

Save edstenson/1237903 to your computer and use it in GitHub Desktop.
Websockets example
var ws = new WebSocket('ws://websocket.datasift.com/<hash>?username=<username>&api_key=<api_key>');
ws.onopen = function(evt) {
// connection event
}
ws.onmessage = function(evt) {
// parse received message
console.log(evt.data);
}
ws.onclose = function(evt) {
// parse event
}
// No event object is passed to the event callback, so no useful debugging can be done
ws.onerror = function() {
// Some error occurred
}
@mheap
Copy link

mheap commented Jun 10, 2014

{"status": "failure", "message": "Something goes here"}
{"status": "warning", "message": "Something goes here"} 
{"status": "info",    "message": "Something goes here"}
{"status": "success", "message": "Something goes here"}

For the above messages, there may also be an unlimited number of arbitrary keys e.g.:

{"status": "warning", "message": "Something goes here", "foo": "bar"}

On initial connection, we send a tick with a status of "initialised":

{"tick": 1234567890, "status": "initialised", "message": "Waiting for data"}

Then if there is no data for 30 seconds, we send another tick, but with a status of "connected"

{"tick": 1234567890, "status": "connected", "message": "Waiting for data"}

Finally, we also send normal interactions, new line delimited

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment