Websockets encoding data to be send

websocket send binary data
websocket send binary data python
websocket send example
javascript websocket
websocket example
websocket client
websocket server

I'm trying to encode the data that I will send to the browser using websockets. I base myself on this tutorial:

private function encode($text) {
    // 0x1 text frame (FIN + opcode)
    $b1 = 0x80 | (0x1 & 0x0f);
    $length = strlen($text);

    if($length > 125 && $length < 65536)
        $header = pack('CCS', $b1, 126, $length);
    elseif($length >= 65536)
        $header = pack('CCN', $b1, 127, $length);

    return $header.$text;

Can anyone tell me what's wrong with this function because I'm not getting any data in web browser. I use it on this line:

    $msg = $this->encode($msg);


PS: I'm not good with binary actions.

@Dharman, I used that same code originally to encode data in my WebSocket script, and it won't work in some cases even with your corrections. The problem is that the length of the payload (data) is not properly calculated. In other words, you can't just use the strlen function.

Please see my related post for details on how to fix this: What is this data at the end of WebRTC candidate info?

WebSocket.send(), The WebSocket.send() method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the� Once you get a Web Socket connection with the web server, you can send data from browser to server by calling a send() method, and receive data from server to browser by an onmessage event handler. Following is the API which creates a new WebSocket object. var Socket = new WebSocket(url, [protocal] );

I solved it. The if statements were wrong. That's how they should look like:

    if($length <= 125)
        $header = pack('CC', $b1, $length);
    elseif($length > 125 && $length < 65536)
        $header = pack('CCS', $b1, 126, $length);
    elseif($length >= 65536)
        $header = pack('CCN', $b1, 127, $length);

Writing WebSocket servers, Either the client or the server can choose to send a message at any Let's call the data ENCODED, and the key MASK. For WebSocket communications, UTF-8 must be used over the wire for textual data (most Internet protocols use UTF-8 nowadays). That is dictated by the WebSocket protocol specification: After a successful handshake, clients and servers transfer data back and forth in conceptual units referred to in this specification as "messages".

Dharman's corrected if statements fail for strings over 125 characters. The correct if statements are:

    if($length <= 125) {
        $header = pack('CC', $b1, $length);
    } elseif ($length > 125 && $length < 65536) {
        $header = pack('CSC', $b1, 126, $length);
    } elseif ($length >= 65536) {
        $header = pack('CNC', $b1, 127, $length);

RFC 6455 - The WebSocket Protocol, _ The protocol has two parts: a handshake and the data transfer. A |Sec- WebSocket-Key| header field with a base64-encoded (see Section 4 of [ RFC4648])� To send a message to the server, simply call "send" and provide the content you wish to deliver. After sending the message, call "close" to terminate the connection, as shown in the following example. As you can see, it really couldn't be much easier. myWebSocket.send("Hello WebSockets!"); myWebSocket.close();

WebSockets - Send & Receive Messages, WebSockets - Send & Receive Messages - The Message event takes place Messages sent by the server to the client can include plain text messages, binary data, o. DOCTYPE html> <html> <meta charset = "utf-8" /> <title>WebSocket� websockets encoding and decoding functions. GitHub Gist: instantly share code, notes, and snippets.

WebSocket, WebSocket is a computer communications protocol, providing full-duplex communication To establish a WebSocket connection, the client sends a WebSocket the client sends a Sec-WebSocket-Key header containing base64- encoded server can send WebSocket data or text frames back and forth in full- duplex mode. If packet_length is 126, the following 2 bytes give the length of data to be read. If packet_length is 127, the following 8 bytes give the length of data to be read. The mask is contained in the following 4 bytes (after the length). The message to be decoded follows this. The data framing section of the spec has a useful illustration of this.

Browser APIs and Protocols: WebSocket, �Sending Text and Binary Data. Once a WebSocket connection is established, the client can send and� Once the connection is established, the client and server can send WebSocket data or text frames back and forth in full-duplex mode. The data is minimally framed, with a small header followed by payload. WebSocket transmissions are described as "messages", where a single message can optionally be split across several data frames.

  • Full complete code here
  • I don't understant how you get the length of the message? What is the character at position 1 telling you about the lenght if the message is a plain string?
  • The character at position 1 (i.e., the second byte of the message) is a combination of one bit denoting whether the message is masked (i.e., encoded) and seven bits denoting the payload (i.e., data) length. The first bit must always be 1 according to the specs because the message must always be masked. The next seven bits when converted to a decimal give you the length. However, because you can only go up to 127 with seven bits, there must be a way of getting the length of messages that are longer than that. According to the specs, if the message is longer than 125 characters...
  • Then the seven bits marking the payload length must be 126, and the actual length is then stored in the 3rd and 4th bytes in the message. If the message is longer than 65,535 characters, then the seven bits marking the payload length must be 127, and the actual length is stored in the 3rd through 8th bytes. In order to convert the binary bits that make up the bytes into a decimal for the length, you have to use the bitwise operators as I have done in the solution linked above...
  • And in order to get the length stored in the last seven bits of the 2nd byte in the message, you need to use logical AND (i.e., the & operator) to essentially ignore the 1 set for the first bit used to denoted whether the message is masked or not. For details on how data is framed, please see the official WebSocket specs. Also, if you need info on what bitwise operators are and how to use them in PHP, please see the following: php.net/manual/en/language.operators.bitwise.php
  • So what your saying is that all strings have this header already or I need to create this header?
  • This fails for strings over 125 characters. The correct code is: ` if($length <= 125) { $header = pack('CC', $b1, $length); } elseif ($length > 125 && $length < 65536) { $header = pack('CSC', $b1, 126, $length); } elseif ($length >= 65536) { $header = pack('CNC', $b1, 127, $length); } `
  • Yes, Brandon. You are right that my if statements are wrong, they were wrong when I copied them from the tutorial. I am planning to take my tutorial down, because it is no longer correct and full of mistakes. I can't even remember this SO question and didn't know it was here. :) However if you would like to correct it for future readers, please include explanation of your fix.