Closed cboden closed 7 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.
Any progress with the binary support? Can I help somehow with this issue?
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.
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.
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/)
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.
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.
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?
@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 :)
Any progress on this front? I would be interested in this as well.
@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?
https://github.com/dizard/Ratchet added binary support for RFC6455 onBinaryMessage + sendBinary() Tested with shorts and long messages
@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?
You are need add to composer.json "repositories": [ { "type":"git", "url":"https://github.com/dizard/Ratchet" } ]
@dizard thank you for your help. I think, I need more to learn about composer.
@dizard Hi I am confused why it is like this when I run composer require dizard/ratchet. Thanks! Problem 1
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
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.
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.
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.