Flask-SocketIO send images


I am currently working on a project that uses Flask-SocketIO to send things over the internet, but I came across this question.


Is there any way to send images in Flask-SocketIO? I did some googling but no luck for me.


Socket.IO is a data agnostic protocol, so you can send any kind of information. Both text and binary payloads are supported.

If you want to send an image from the server, you can do something like this:

with open('my_image_file.jpg', 'rb') as f:
    image_data = f.read()
emit('my-image-event', {'image_data': image_data})

The client will have to be aware that you are sending jpeg data, there is nothing in the Socket.IO protocol that makes sending images different than sending text or other data formats.

If you are using a JavaScript client, you will get the data as a byte array. Other clients may choose the most appropriate binary representation for this data.

Answered By – Miguel

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published