javascript - Error handling over websockets a design dessision -


im building webapp has 2 clear use cases.

  1. traditional client request data server.
  2. client request stream server after wich server starts pushing data client.

currently im implementing both 1 , 2 using json message passing on websocket. has proven hard since need handcode lots of error handling since client not waiting response. sends message hoping reply sometime. im using js , react on frontend , clojure on backend.

i have 2 questions regarding this.

  1. given current design, alternatives there error handling on websocket?
  2. would smarter split 2 uc using rest uc1 , websockets uc2 use fetch on frontend rest calls.

update. current problem not knowing how build async send function on websockets can match send messages , response messages.

here's scheme doing request/response on socket.io. on plain websocket, you'd have build little more of infrastructure yourself. same library can used in client , server:

function initrequestresponsesocket(socket, requesthandler) {     var cntr = 0;     var openresponses = {};      // send request     socket.sendrequestresponse = function(data, fn) {         // put data in wrapper object contains request id         // save callback function id         var id = cntr++;         openresponses[id] = fn;         socket.emit('requestmsg', {id: id, data: data});     }      // process response message comes request     socket.on('responsemsg', function(wrapper) {         var id = wrapper.id, fn;         if (typeof id === "number" && typeof openresponses[id] === "function") {             fn = openresponses[id];             delete openresponses[id];             fn(wrapper.data);         }     });      // process requestmsg     socket.on('requestmsg', function(wrapper) {         if (requesthandler && wrapper.id) {             requesthandler(wrapper.data, function(responsetosend) {                 socket.emit('responsemsg', {id: wrapper.id, data; responsetosend});             });         }     }); } 

this works wrapping every message sent in wrapper object contains unique id value. then, when other end sends it's response, includes same id value. id value can matched particular callback response handler specific message. works both ways client server or server client.

you use calling initrequestresponsesocket(socket, requesthandler) once on socket.io socket connection on each end. if wish receive requests, pass requesthandler function gets called each time there request. if sending requests , receiving responses, don't have pass in requesthandler on end of connection.

to send message , wait response, this:

socket.sendrequestresponse(data, function(err, response) {     if (!err) {         // response here     } }); 

if you're receiving requests , sending responses, this:

initrequestresponsesocket(socket, function(data, respondcallback) {    // process data here     // send response    respondcallback(null, yourresponsedata); }); 

as error handling, can monitor loss of connection , build timeout code if response doesn't arrive in amount of time, you'd error back.

here's expanded version of above code implements timeout response not come within time period:

function initrequestresponsesocket(socket, requesthandler, timeout) {     var cntr = 0;     var openresponses = {};      // send request     socket.sendrequestresponse = function(data, fn) {         // put data in wrapper object contains request id         // save callback function id         var id = cntr++;         openresponses[id] = {fn: fn};         socket.emit('requestmsg', {id: id, data: data});         if (timeout) {             openresponses[id].timer = settimeout(function() {                 delete openresponses[id];                 if (fn) {                     fn("timeout");                 }             }, timeout);         }     }      // process response message comes request     socket.on('responsemsg', function(wrapper) {         var id = wrapper.id, requestinfo;         if (typeof id === "number" && typeof openresponse[id] === "object") {             requestinfo = openresponses[id];             delete openresponses[id];             if (requestinfo) {                 if (requestinfo.timer) {                     cleartimeout(requestinfo.timer);                 }                 if (requestinfo.fn) {                     requestinfo.fn(null, wrapper.data);                 }             }         }     });      // process requestmsg     socket.on('requestmsg', function(wrapper) {         if (requesthandler && wrapper.id) {             requesthandler(wrapper.data, function(responsetosend) {                 socket.emit('responsemsg', {id: wrapper.id, data; responsetosend});             });          }     }); } 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -