While communicating through WebSockets, there are various kinds of data we might transfer between servers and clients. These data can be mainly classified into two categories as text data and binary data. When we are sending some strings of messages which can be printed in textual format as it is, they are text data. We have to deal with binary data when we have things such as files, multimedia, etc to be transferred between WebSocket clients and servers. The focus of this blog post is to share some information which I explored recently. Its about transferring some binary data through WebSockets however not as binary data but instead as textual data. How exactly we do such a thing. This is where Base64 encoding/decoding fits in.
Base64 is a way to encode binary data as ASCII text strings so that we can transfer them through channels which are specifically designed to transfer texts. For example, a WebSocket client can take an image file, encode it in Base64 and transfer it as a text stream through a WebSocket to a server. The server can decode the text stream and regenerate the binary patterns that resembles the image file at server side. That is the scenario which I will show with example codes in this article. I will leave the further readings about Base64 up to interested parties. There are lot of on-line articles to read about it and a good place to understand it simply is this Wikipedia article.
Now it's time to try some example codes. This example assumes that you have installed and tried Autobahn WebSocket library in your Linux box already. If not, checkout this article which I wrote a couple of weeks back. Once you have settled everything with Autobahn installation and tried the echo client / server example, we are ready to begin this exploration. We need three things to try a file transferring scenario between a server and a client through a WebSocket. Those are, a client program, a server program and a file to be transferred. We will write the server and client programs in Python language using the Autobahn Python library. The source codes shown below can also be downloaded from GitHub.
Client program code ( client.py )
Server program code ( server.py )
As you can see from the client program code, we need to have the image.jpg file which is supposed to be transferred from the client, in the same directory as client.py file. Let's run the programs and see how they work. Open two terminals and run the server and client programs in those two separate terminals. Server will create a new image file named as image_received.jpg based on the data stream it received through the WebSocket.
This simple example demonstrate how we can transmit binary data such as image files through WebSockets as textual data. Even though we are allowed to transmit binary data through WebSockets as it is, there can be practical scenarios where we have to restrict the communication to text only. In such situations, Base64 can play a great role to assist us on encoding and decoding binary data with textual data.