13

I am making a little experiment with WebSockets and Java. Apperantly, according to the last draft of WebSocket, the message can be binary or a plain string. I use Webbit server and it has two functions:

public void onMessage(WebSocketConnection connection, String message) 

public void onMessage(WebSocketConnection connection, byte[] message)

I wonder what makes a difference. Is byte[] faster? Or why does it matter? I can write everything I write with the bytes because even a string is composed into bytes while transfer, so why do we have two multiple methods? Only Google Chrome 15 Beta and 16 Dev supports binary transfer, so I was thinking of using Base64 encoding/decoding on both client and server. Is this the only difference? What if I just read each byte, compose them into a string and send them? I think, only difference will be that not all bytes are String chars, so I wouuld just add an overhead when converting to a String?

tl;dr -> What is the difference between Binary transfer and String transfer?

Mustafa
  • 10,013
  • 10
  • 70
  • 116
  • 3
    How many questions are you asking!? – Mob Oct 11 '11 at 17:54
  • @Mob, I am just confused, actually it is one question, What are the differences? I just exploded the question to be more clear. Will paraphrase if you wish. – Mustafa Oct 11 '11 at 17:58

2 Answers2

13

The WebSocket protocol (HyBi) supports two different payload types: text, binary. The text payload is UTF-8 encoded string data. Any ASCII codes above 127 in the string that you send will be converted into a two-byte UTF-8 encoding. To successfully send/receive raw binary data you will probably want to encode the data in something like base64 (which is UTF-8 compatible).

The binary payload type is sent directly. The bytes are sent as-is in the payload. This is more bandwidth efficient. It is means that you don't have to do an encode/decode step. The bytes you send get sent directly, and the bytes you receive can be accessed directly with no decoding.

kanaka
  • 70,845
  • 23
  • 144
  • 140
  • bytes received can be accessed directly : is that true for all clients like android (okhttp3) and ios (starscream).. – DragonFire May 14 '20 at 01:45
1

When you are using binary mode there is no UTF-8 conversion of the data before sending it. As far as speed, there is very little difference as UTF-8 encoding of text is very fast and negligible. It looks like they give you two options so you can send that data either way depending on what data type you want to send.

Zectbumo
  • 4,128
  • 1
  • 31
  • 26
Matt
  • 27
  • 1