I created this extension for one of my projects, where I use it together with my other extension AudioStream to send and receive audio between an ESP32 and a smartphone.
What it is for
WebSocketExt is a non-visible extension that opens a WebSocket connection: a persistent, bidirectional connection to a ws:// or wss:// server.
App Inventor’s Web component only makes single HTTP requests: the app asks, the server answers, and the connection ends. With a WebSocket the connection stays open, and both sides can send messages at any time. The server can notify the app as soon as something happens, without the app having to keep asking. This is useful for chats, real-time commands, server notifications, IoT device control and streaming.
The extension keeps the connection alive by sending a ping every 15 seconds.
Text messages and binary messages
Text: SendText sends any string, for example JSON. Incoming text messages fire TextMessageReceived.
Binary: binary messages follow a simple convention. The first byte of each message identifies its type:
0x02= audio chunk →AudioChunkReceivedevent; send one withSendAudioChunk
The type byte is added automatically when sending and removed when receiving, so the events only contain the data. Binary messages with any other first byte are ignored. To use binary messages, the server must follow the same convention.
Binary data is a list of numbers from 0 to 255. This is the same format AudioStream uses, so chunks can be passed between the two extensions without any conversion.
General usage
- Call
Connectwith the server address (ws://…orwss://…). - When
Connectedfires, the connection is ready and you can start sending messages. - Incoming messages arrive in
TextMessageReceived,AudioChunkReceivedandVideoFrameReceived. - To close the connection, call
Disconnect. - Always handle
Error, which also reports a lost connection.

Opens a connection to the given address (ws:// or wss://). If a connection is already open, it is closed before the new one is opened. On success Connected fires, otherwise Error.

Closes the connection.

Returns true if the connection is currently open.

Sends a text message to the server, for example a command or a JSON object. If not connected, Error fires.

Sends an audio chunk as a binary message. The bytes parameter is a list of numbers from 0 to 255; the 0x02 type byte is added automatically. It can be connected directly to AudioStream’s AudioChunkCaptured event. If not connected, Error fires.

Fires when the connection is open and ready. This is the right place to send the first messages, for example an identification message to the server.
Fires when the connection is closed normally, by either side.
code and reason are the WebSocket protocol close code and reason. If the connection drops because of a network problem, Error fires instead.
Fires for each text message received.
text holds the message.
Fires for each audio chunk received (a binary message whose first byte is
0x02). bytes is a list of numbers from 0 to 255, without the type byte. It can be passed directly to AudioStream’s PlayChunk.
Fires when the connection fails, when it drops (network lost, server unreachable) and when you try to send while not connected.
message describes the problem.
com.websocketext.aix (634.5 KB)
Example: two-way audio with AudioStream
Connectto the server; inConnected, call AudioStream’sStartPlaybackandStartRecording.AudioStream.AudioChunkCaptured→WebSocketExt.SendAudioChunk.WebSocketExt.AudioChunkReceived→AudioStream.PlayChunk.- When finished:
StopPlayback,StopRecordingandDisconnect.
I hope you find useful
Best regards
Marco Perrone



