Skip to content

Commit

Permalink
feat: handles edge case for second subscribe call and adds todo for h…
Browse files Browse the repository at this point in the history
…andling client disconnect on server side
  • Loading branch information
Martin-Ting committed Dec 20, 2016
1 parent 235c5ba commit 1cd5984
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
36 changes: 14 additions & 22 deletions experimentation/sockets.js
Expand Up @@ -2,38 +2,30 @@ var socket;
var socketid;

function subscribe(uri, query, variables = null, callback) {
console.log(`subscribing to ${query}`);
if (!socket) {
socket = io('http://localhost:4000');
socket = io(uri);
socket.on('init', ({id}) => {
socketid = id;
socket.on(socketid, (data) => { callback(data) });
socket.emit(socketid, { query });
});
}else{
socket.emit(socketid, {query});
}

// var xhr = new XMLHttpRequest();
// xhr.responseType = 'json';
// xhr.open("GET", uri, false);
// xhr.setRequestHeader("Content-Type", "application/json");
// xhr.setRequestHeader("Accept", "application/json");
// xhr.send(JSON.stringify({
// query: query,
// variables: variables
// }));
// return xhr.responseText;
}

function unsubscribe() {
socket.emit('unsubscribe', { socketid });
}
// subscribe(null, '{ getMessages(id: 0, test:"testarg", another:"anotherarg", something:"somethingarg"){content, author} }', null, function (data) { console.log(data) });

//subscribe(null, '{ getMessages(id: 0, test:"testarg", another:"anotherarg", something:"somethingarg"){content, author} }', null, function (data) {
subscribe(null, `
{
getMessage(id: 0){
id, content, author
}
}
`, null, function (data) {
console.log(data);
});
// subscribe(null, `
// {
// getMessage(id: 0){
// id, content, author
// }
// }
// `, null, function (data) {
// console.log(data);
// });
2 changes: 1 addition & 1 deletion src/jobqueue.js
Expand Up @@ -23,7 +23,7 @@ class JobQueue {
return this.jobQueue.length;
}

addObservable(name, callback, errCallback, completeCallback, loopback=true, interval = 5000) {
addObservable(name, callback, errCallback, completeCallback, loopback=true, interval = 100) {
if(!this.watchdogs[name]) {
let subscribeCallback = (intervalTime) => {
if(this.jobQueue.length > 0) {
Expand Down
7 changes: 6 additions & 1 deletion src/subql.js
Expand Up @@ -123,7 +123,12 @@ function handleSubscribe(query, socketid) {
query.query,
root
).then((result) => {
connected[socketid].socket.emit(socketid, result);
if(connected[socketid]){
connected[socketid].socket.emit(socketid, result);
}else{
// client has disconnected.
// TODO add in logic for removing data about disconnected client
}
});
}

Expand Down

0 comments on commit 1cd5984

Please sign in to comment.