SocketReadMessage[socket]
reads the next available message on the specified socket, returning it as a byte array.
SocketReadMessage
SocketReadMessage[socket]
reads the next available message on the specified socket, returning it as a byte array.
Details and Options
- SocketReadMessage supports the following option:
-
"MultipartComplete" True whether to read a complete message or only a fragment - The option "MultipartComplete" will only have an effect for web sockets.
- The socket is specified as a SocketObject[…].
- For ZMQ sockets, SocketReadMessage returns the next discrete message sent to the socket. It returns all multi-part elements joined together into a single ByteArray.
- For TCP sockets, SocketReadMessage will return a ByteArray of the data currently in the operating system buffer.
- For web sockets, SocketReadMessage will return an Association with the following keys:
-
"DataByteArray" the payload of the message, as a ByteArray "Type" the type of the message, either "Text" or "Binary" "MultipartComplete" whether there are additional fragments in the message - A value of True for the key "MultipartComplete" corresponds to no additional fragments. The value of this key will always be True if the option "MultipartComplete" is also True.
Examples
open all close allBasic Examples (3)
Open a ZMQ socket and connect a client to it:
server = SocketOpen[Automatic, "ZMQ"];
client = SocketConnect[{server["DestinationIPAddress"], server["DestinationPort"]}, "ZMQ"];Writing a message from one side will send the message to the other side as a ByteArray:
WriteString[client, "hello from the client"]SocketReadMessage[server]Turn it into a string with ByteArrayToString:
ByteArrayToString[%]Close /@ {server, client}For TCP sockets, the size of the message returned by SocketReadMessage is not deterministic and is controlled by the operating system:
server = SocketOpen[Automatic, "TCP"];
client = SocketConnect[server];Write out a large message from the server to the client:
WriteString[First@server["ConnectedClients"], StringJoin@Table["a", 2000]]Read messages from the socket until there is no more data available:
First@Last@Reap[While[SocketReadyQ[client], Sow[SocketReadMessage[client]]]]Confirm that the individual buffers add up to the entire message sent on the socket:
Total[Length /@ %]Close /@ {server, client}socket = SocketConnect["wss://echo.websocket.org"];
msg = SocketReadMessage[socket]Convert the "DataByteArray" to a string:
ByteArrayToString[msg["DataByteArray"]]Scope (1)
With ZMQ sockets, a single message is sent for every expression passed to WriteString:
server = SocketOpen[Automatic, "ZMQ"];
client = SocketConnect[server]WriteString[server, Alphabet[]]ByteArrayToString@SocketReadMessage[client]Expressions can be written with Write and read back with Read, where each individual message sent is read back as a complete expression:
Do[Write[client, Graphics[{RandomColor[], Disk[]}]], 10]Table[Read[server], 10]Expressions are written on the socket by default as a string in InputForm:
Write[server, RandomImage[1, 5]]ByteArrayToString@SocketReadMessage[client]ToExpression[%]When writing expressions with Write, an additional newline message will appear in between expressions:
Do[Write[server, f[x]], 3];
First@Last@Reap[While[SocketReadyQ[client], Sow[ByteArrayToString[SocketReadMessage[client]]]]]Applications (1)
Define a flush function to remove all data sent on a socket:
server = SocketOpen[Automatic, "ZMQ"];
client = SocketConnect[server];flush[sock_] := While[SocketReadyQ[sock], SocketReadMessage[sock]]i = 1;
Do[WriteString[server, i++], 1000]There are now messages on the socket available to read:
SocketReadyQ[client]ByteArrayToString@SocketReadMessage[client]Flush all of the messages to read new messages:
flush[client]Now there is no data available; new data that is sent will show up first:
SocketReadyQ[client]WriteString[server, i++]ByteArrayToString@SocketReadMessage[client]Close /@ {server, client}Neat Examples (1)
Open up a TCP socket connection:
sock = SocketOpen[Automatic]Connect a web browser to see the HTTP request written on the socket by the browser:
SystemOpen@URLBuild[<|"Scheme" -> "http", "Port" -> sock["DestinationPort"], "Domain" -> First@sock["DestinationIPAddress"]|>]Nothing will appear in the browser window because we have not sent any HTTP response.
See the connected clients (there might be more than one, depending on your browser):
sock["ConnectedClients"]Read the requests from any ready sockets:
ByteArrayToString /@ SocketReadMessage /@ Select[sock["ConnectedClients"], SocketReadyQ]Close the connections to the clients:
Close /@ sock["ConnectedClients"]Related Guides
History
Text
Wolfram Research (2017), SocketReadMessage, Wolfram Language function, https://reference.wolfram.com/language/ref/SocketReadMessage.html.
CMS
Wolfram Language. 2017. "SocketReadMessage." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SocketReadMessage.html.
APA
Wolfram Language. (2017). SocketReadMessage. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SocketReadMessage.html
BibTeX
@misc{reference.wolfram_2026_socketreadmessage, author="Wolfram Research", title="{SocketReadMessage}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/SocketReadMessage.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_socketreadmessage, organization={Wolfram Research}, title={SocketReadMessage}, year={2017}, url={https://reference.wolfram.com/language/ref/SocketReadMessage.html}, note=[Accessed: 12-June-2026]}