Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a front end project that is using socket.io for all communication with t

ID: 647236 • Letter: T

Question

This is a front end project that is using socket.io for all communication with the server. This allows for push which is good, but recently we have been asked to provide a pause button. The pause button is intended on stopping the data from changing out on the ui if the user does not wish it to do so automatically.

We currently have a class that wraps the web-socket and provides very minimal wrapping of socket.io.

The problem is, we can't just turn off the socket. User interactions need to continue to work, and different views have different specs as to what messages need to be ignored.

Ultimately its a big global boolean no matter how you slice it, and that sucks. I would rather not have it truly be a global variable, so here are the idea's I've had so far:

Narsty Global

// A non pausable listener
socket.on('myEvent', function(data){
      // do thing
});

// A pausable listener
socket.on('myPausable', function(data){
      if(window.isPaused){ return false; }
      // do thing
});
Passed Value

// A non pausable listener
socket.on('myEvent', function(data){
      // do thing
});

// A pausable listener
socket.on('myPausable', function(data, isPaused){
      if(isPaused){ return false; }
      // do thing
});
Wrapped Method

// A non pausable listener
socket.on('myEvent', function(data){
      // do thing
});

// A pausable listener
socket.onLive('myPausable', function(data){
      // do thing
});
Separate Class

// A non pausable listener
socket.on('myEvent', function(data){
      // do thing
});

// A pausable listener
liveData.on('myPausable', function(data){
      // do thing
});
Currently there is ALOT of messaging taking place over the socket, and there is only going to be one 'PAUSE' toggle on the UI, so this needs to be done carefully. Advice and best practices are appreciated. Thanks folks.

Explanation / Answer

I have some cognitive dissonance regarding the pause button - as a user, I consider 'Pause' to mean - halt what the server is doing. From your requirement, though, it seems that the server is not affected by the button at all, which might be confusing, and even misleading, as the feedback to the user really stops. I believe that in most cases, a button called 'Stop animation' or a toggle called 'Auto-refresh' might convey the idea to the user better.

To the matter at hand, I could suggest one more idea, which is having a flag on the server, which will prevent relevant messages from reaching the client altogether when paused. This way, the only change in the client is adding the server message to pause or un-pause, and the rest stays the same.

If that does not make sense in your case, I believe that the simplest solution (although deemed ugly) will be the best, since it has the least number of moving parts. Use a global state variable (under a suitable namespace of course - window.MyAmazingApp.isPaused), and use it as necessary, leaving other code untouched.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote