Skip to content

Instantly share code, notes, and snippets.

@bharathvaj-ganesan
Last active October 31, 2021 14:47
Show Gist options
  • Save bharathvaj-ganesan/509dd0942f5bed492e21cfa462aa120d to your computer and use it in GitHub Desktop.
Save bharathvaj-ganesan/509dd0942f5bed492e21cfa462aa120d to your computer and use it in GitHub Desktop.
$(function () {
// if user is running mozilla then use it's built-in WebSocket
window.WebSocket = window.WebSocket || window.MozWebSocket;
const connection = new WebSocket('ws://localhost:8080/githubEvents');
connection.onopen = function () {
// connection is opened and ready to use
};
connection.onerror = function (error) {
// an error occurred when sending/receiving data
};
connection.onmessage = function (message) {
// try to decode json (I assume that each message
// from server is json)
try {
const githubEvent = JSON.parse(message.data); // display to the user appropriately
} catch (e) {
console.log('This doesn\'t look like a valid JSON: '+ message.data);
return;
}
// handle incoming message
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment