ratchetphp / Ratchet

Asynchronous WebSocket server
http://socketo.me
MIT License
6.28k stars 743 forks source link

Binary support #18

Closed cboden closed 7 years ago

cboden commented 12 years ago

Part of #13 is including binary message support for the WS protocol.

I'm thinking this will expand on the WebSocketComponent interface addax an onFile method. Try to further incorporate SF2 HttpFoundation by passing a File object to the method.

cboden commented 12 years ago

The context of this ticket was originally for incoming/uploaded files from the client. Outgoing binary messages will also require some thought.

Also consider "binary" may not necessarily be a file, but binary data. SF File would not make sense in that case.

Look into stream support for both incoming/outgoing binary messages.

scorq commented 11 years ago

Any progress with the binary support? Can I help somehow with this issue?

cboden commented 11 years ago

Absolutely you can help! :) Code and ideas are always welcome. Here's what's holding up this feature:

Most of the work at the protocol level has already been done. What's left to do is define how the API should work through the rest of the application. These are what I've come up with so far, but not sure which I'd prefer.

1) Instead of calling onMessage with a string value pass a MessageInterface instead and let the end developer check if it's binary or text type. Keeps the parallel to the Javascript 4-method API but could make the end-developer less aware.

2) Create a new method like onBinaryMessage to pass binary data through the layers. Very explicit, might incorporate better with WAMP component as I don't believe v1 spec handles binary.

3) Another possibility?


I haven't read too much up on this part, but there are 2 types of binaryTypes: Blob and ArrayBuffer. Blob is just a binary string, but I'm not quite sure what an ArrayBuffer is and if/how it looks different on the server side of the wire. That would need to be researched as well.

scorq commented 11 years ago

I think it would be useful to have both solutions (1/ and 2/): the obvious onBinaryMessage and an additional onRawMessage with the MessageInterface as a parameter. The second method could be called before onMessage or onBinaryMessage and would allow developers to process a message on a lower level.

As for the Blob and ArrayBuffer are you referring to these JavaScript types? https://developer.mozilla.org/en-US/docs/Web/API/Blob https://developer.mozilla.org/en-US/docs/Web/API/ArrayBuffer

AFAIK there is no difference at the wire level. You just receive/send a bunch of bytes regardless of a wrapper (Blob or ArrayBuffer) used at the client side.

vincentdieltiens commented 11 years ago

I'm also interested in this feature. Wrench is also a websocket library and you can see what is their choice : https://github.com/varspool/Wrench/blob/03d1f1dc012687eea9bfcaebbf16300bda001a22/lib/Wrench/Connection.php

If the type of the message is text, it call the server onData method If the type of binary, it checks the existence of the onBinaryData method and call it. If method doesn't exists, it call onData

IMHO, I don't think having both 1/ and 2/ is a good idea. I always prefer a "one way to do things" because, it's better when several developper using (in the same team).

The two solutions are OK. But as @cboden says, the 2/ is more explicit.

Concerning Blob and ArrayBuffer : I think the data received by the server is the same in case of Blob orArrayBuffer (http://blog.ericzhang.com/state-of-binary-in-the-browser/)

vincentdieltiens commented 10 years ago

I've started to implement the second solution with onBinaryMessage methods : https://github.com/vincentdieltiens/Ratchet/tree/binary-support

It's working for websockets that does not using Hixies76, because I don't know what to do in the onBinaryMessage method.

cboden commented 10 years ago

Thanks for taking the initiative on this @vincentdieltiens!

The Hixie76 protocol does not support binary messages. If they need to support a onBinaryMessage method for interface constraints you can just throw something like an OutOfBoundsException inside.

scorq commented 10 years ago

This is a great work. Thanks! What about sending binary messages? The AuthobanPython server uses the same sendMessage() method to send both text and binary messages. The difference is only an optional binary flag. What has to be done in Ratchet to enable this?

https://autobahnpython.readthedocs.org/en/latest/websocketbase.html#autobahn.websocket.WebSocketProtocol.sendMessage

vincentdieltiens commented 10 years ago

@scorq : I've just added the possibility to know when a binary message is received. But I think that ConnectionInterface::send supports sending binary strings. It can be tested with the Autobahn and the EchoServer (it resends also binary messages).

As I don't have installed Autobahn yet (never worked with it), I don't know if all tests passes.

@cboden : thanks for the information, I will throw an exception so :)

stewartadam commented 10 years ago

Any progress on this front? I would be interested in this as well.

nazar-pc commented 9 years ago

@vincentdieltiens, are you going to send PR for this feature? I need to receive binary messages as well, so what it implementation state right now?

dizard commented 8 years ago

https://github.com/dizard/Ratchet added binary support for RFC6455 onBinaryMessage + sendBinary() Tested with shorts and long messages

khalilst commented 8 years ago

@dizard I've a problem in installing your package via composer in my Laravel application. composer require dizard/ratchet But it's not working. Composer shows this error message:

Could not find package dizard/ratchet at any version for your minimum-stability (stable). Check the package spelling or your minimum-stability

I'm totally confused! What was wrong with this package?

dizard commented 8 years ago

You are need add to composer.json "repositories": [ { "type":"git", "url":"https://github.com/dizard/Ratchet" } ]

khalilst commented 8 years ago

@dizard thank you for your help. I think, I need more to learn about composer.

mengnalin90 commented 7 years ago

@dizard Hi I am confused why it is like this when I run composer require dizard/ratchet. Thanks! Problem 1

ryancwalsh commented 5 years ago

Does Ratchet definitely support sending binary data? IBM Watson Speech-to-text requires it: https://console.bluemix.net/docs/services/speech-to-text/websockets.html#WSaudio

The client must send the audio as binary data. The client can stream a maximum of 100 MB of audio data with a single utterance (per send request). It must send at least 100 bytes of audio for any request. The client can send multiple utterances over a single WebSocket connection.

The WebSocket interface imposes a maximum frame size of 4 MB. The client can set the maximum frame size to less than 4 MB. If it is not practical to set the frame size, the client can set the maximum message size to less than 4 MB and send the audio data as a sequence of messages.

Looking at http://socketo.me/docs/hello-world and other docs, I've been unclear on how to use Ratchet to do something super basic like this (but with support for binary): https://github.com/Textalk/websocket-php/blob/master/examples/send.php

ryancwalsh commented 5 years ago

I found https://github.com/ratchetphp/Ratchet/issues/393#issuecomment-329532173 and https://github.com/ratchetphp/Ratchet/issues/627#issue-304813937 and am just as confused as @SilverDeath32.

I'm wondering how to put an audio file into:

$binaryMsg = new \Ratchet\RFC6455\Messaging\Message();
$conn->send($binaryMsg);

I will ask on his issue. Thanks for this library, which looks promising.

programarivm commented 2 months ago

Hi there,

This one is working for me.

https://github.com/chesslablab/chess-server/blob/main/src/Socket/Ratchet/BinaryWebSocket.php

I hope it helps.