Is it advisable to send images url from the server to the client using socket.io and then on the client side ,user listens to the image emit event and display on the web page ? Why do we need to base64 encode the image on the server-side and then send it to the client when you have the easier way of emitting image url to thwe client . !
-
It would make a lot more sense to me to send an image URL via socket.io and then let the browser use a normal HTML image to load the image from your web server. – jfriend00 Apr 04 '18 at 16:08
-
sorry i didnt understand you – roh_dev Apr 04 '18 at 16:09
-
I guess i said the same thing in the question – roh_dev Apr 04 '18 at 16:10
1 Answers
If there's no technical reason why you can't just send an URL via socket.io and have your client code then create an image object and set the .src
to that URL to let the browser fetch the image the standard way via an URL, then that would definitely be preferred. You, of course, would have to make sure that the web server was prepared to serve that image when presented with the URL and if the image is unique content for that user, then the URL would also have to be unique.
That lets the browser use it's most optimized code for downloading binary images, lets the browser cache them, lets other network infrastructure like proxies do their optimizations, etc...
Why do we need to base64 encode the image on the server-side and then send it to the client when you have the easier way of emitting image url to the client?
You don't.
For example, you can send HTTP responses with binary data in them where you set the content-type
to some binary content type and then set the content-length
to the exact length of the binary data. Here's a minimal example of a JPEG response with binary data:
HTTP/1.1 200 OK
Content-Length: 126750
Content-Type: image/jpeg
<binary image data here>
base64 is a scheme for sending binary data when the transport won't accept binary data. As it sounds like you already know, it's not nearly as efficient as sending actual binary data so when the transport and the recipient can handle binary data for known content types such as images, binary is typically preferred.

- 683,504
- 96
- 985
- 979
-
Thanks for your reply @jfriend00 . I just want to understand when you say NO to the question on why we base64 encode the image on the server side and then emit the binary data to the client – roh_dev Apr 05 '18 at 08:03
-
@roh_dev - You don't have to base64 encode it when using socket.io - that is just one option for sending the data. You can send binary data if you want to. See [How to send binary data with socket.io](https://stackoverflow.com/questions/34056705/how-to-send-binary-data-with-socket-io/). – jfriend00 Apr 05 '18 at 14:41
-
I know how to send binary data. Base64 encoding gives u the binary data only i suppose in the form of string. But my question is when i Google how to emit images from server to clients almost every answer corresponds to base64 encode the image and then emit to the client. This method is just complicating the concept of sending images to the client i suppose when one has the easier way of sending image url to the client and the client uses it in image tag with src attribute – roh_dev Apr 05 '18 at 15:47
-
@roh_dev - An http response must specify what content type the data is in. There are many choices. A base64 encoding is one choice. You can also send raw image types such as `image/jpeg` or plain binary such as `application/octet-stream` and then set a `content-length`that tells the recipient exactly how much data there is. See what I added to my answer for an example. – jfriend00 Apr 05 '18 at 16:52