简体   繁体   中英

Can MQTT stream audio directly to a web client?

I was able to set up an Arduino to stream audio from a microphone into a linux server that's hosting an MQTT server. I then have a golang script that subscribes to the MQTT server, saves payload to disk as a binary file, and converts the binary file to a .WAV file with FFMPEG.

Is it possible to have a web browser use only client side code to subscribe to the same MQTT server, receive the audio payload from the Arduino, and stream the audio in near-real-time to the human listener's computer speakers? I see a Paho Javascript Client library that can help me connect to MQTT, but it seems to receive payloads as string, which isn't evident to me on how I'd stream audio content with. Hence, why I'm asking if it's even practical/feasible?

Or will I need to build another server-side script to stream MQTT data as audio data for a web client?

Look closer at the pahodoc , there is a fiction to get the message payload as binary data using the message.payloadBytes field.

payloadBytes | ArrayBuffer | read only The payload as an ArrayBuffer

An example is described here:

https://www.hardill.me.uk/wordpress/2014/08/29/unpacking-binary-data-from-mqtt-in-javascript/

But basically you end up withan ArrayBuffer holding the binary data that you can then coonvert to a Typed Array and read back values at what ever offset you need.

To ensure it works in all environments, ensure that you use MQTT over WebSocket to connect to the server.

Here is a discussion of this: Can a web browser use MQTT?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM