Socket.io emit event for only emitter

31
June 06, 2019, at 11:20 PM

i have image sending algorithm with socket.io and i decided to add "loading" message til image is loaded.

  $("#output").append(
        `<div id="loading-placeholder" style="opacity: 0"><div id="loading">${loadingSvg}</div></div>`
      );

and this div is removed when image is loadded

but problem is that i have image validation in server side

var ext = image.substring("data:image/".length, image.indexOf(";base64"));
console.log('extension ' + ext)
if(ext === 'jpeg' || ext === 'png' || ext === 'jpg'){
  chnm.in(socket.current_room).emit('img', {image: data.image, id: socket.id});
}
else{
  return false  
}

and it returns false if image extension is not jpeg || png || jpg, and in that case it returns false and image is never loaded so that loading svg stays forever. so if server side validation fails i want to return response to user that his image failed to send. how can i do that?

Answer 1

Using control message. Add is_loaded property.

chnm.in(socket.current_room).emit('img', {
  image: data.image,
  id: socket.id,
  is_loaded: true
});

In case of faliure:

chnm.in(socket.current_room).emit('img', {
  image: null,
  id: null,
  is_loaded: false
});

Another possible solutions:

  • Simply check image for null.
  • Add timeout on client side.
READ ALSO
Unable to read readableStreamBody from downloaded blob

Unable to read readableStreamBody from downloaded blob

I can I check the internal buffer to see if my text data is present? Am I using nodejs' Stream

55
How do you update what you&#39;re working on while developing in angular?

How do you update what you're working on while developing in angular?

When I use angular CLI and do ng serve I am able to make changes and able to instantly see them on the web browserHowever, I am using the MEAN stack and unable to use ng serve

34
Electron: X is not a fuction &gt;&gt;&gt; (Callback using ipcrenderer / ipcmain)

Electron: X is not a fuction >>> (Callback using ipcrenderer / ipcmain)

The Problem I'm using Promise(s) to get a callback when libraries are loadedEach promise sends a message through ipcRenderer to ipcMain and includes a callback function

51
Is this a good way of breaking &ldquo;THEN chain&rdquo; when using promises?

Is this a good way of breaking “THEN chain” when using promises?

I have a simple "then chain" that runs some functinality stepsIf some condition is met I need to cancel the chain and exit (mainly if an error occurrs)

56