({{, (RefLink[SocketReadMessage, paclet:ref/SocketReadMessage])[socket] 
reads the next available message on the specified socket, returning it as a byte array.}})

OpenersNotesSection(Spacer1)Details and Options

The socket is specified as a (RefLink[SocketObject, paclet:ref/SocketObject])[…].

For ZMQ sockets, (RefLink[SocketReadMessage, paclet:ref/SocketReadMessage]) returns the next discrete message sent to the socket. It returns all multi-part elements joined together into a single (RefLink[ByteArray, paclet:ref/ByteArray]).

For TCP sockets, (RefLink[SocketReadMessage, paclet:ref/SocketReadMessage]) will return a (RefLink[ByteArray, paclet:ref/ByteArray]) of the data currently in the operating system buffer.

OpenersPrimaryExamplesSection(Spacer1)Examples  (5)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

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 (RefLink[ByteArray, paclet:ref/ByteArray]):

WriteString[client,"hello from the client"]

SocketReadMessage[server]

Turn it into a string with (RefLink[ByteArrayToString, paclet:ref/ByteArrayToString]):

ByteArrayToString[%]

Close the sockets:

Close/@{server,client}

For TCP sockets, the size of the message returned by (RefLink[SocketReadMessage, paclet:ref/SocketReadMessage]) is not deterministic and is controlled by the OS:

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",1000000]]

Read messages from the socket until there is no more data available:

First@Last@Reap[While[SocketReadyQ[client],Sow[SocketReadMessage[client]];Pause[0.1]]]

Confirm the individual buffers add up to the entire message sent on the socket:

Total[Length/@%]

Close the sockets:

Close/@{server,client}

OpenersExampleSection1(Spacer1)Scope  (1)

With ZMQ sockets, a single message is sent for every expression passed to (RefLink[WriteString, paclet:ref/WriteString]):

server=SocketOpen[Automatic,"ZMQ"];
client=SocketConnect[server]

WriteString[server,Alphabet[]]

ByteArrayToString@SocketReadMessage[client]

Expressions can be written with (RefLink[Write, paclet:ref/Write]) and read back with (RefLink[Read, paclet:ref/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 (RefLink[InputForm, paclet:ref/InputForm]):

Write[server,RandomImage[1,5]]

ByteArrayToString@SocketReadMessage[client]

ToExpression[%]

When writing expressions with (RefLink[Write, paclet:ref/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]]]]]

OpenersExampleSection3(Spacer1)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 the sockets:

Close/@{server,client}

OpenersExampleSection4(Spacer1)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"]|>]

See the connected clients:

sock["ConnectedClients"]

Read the request from the first socket:

ByteArrayToString@*SocketReadMessage/@Select[SocketReadyQ]@sock["ConnectedClients"]

Close the connections to the clients:

Close/@sock["ConnectedClients"]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[SocketConnect, paclet:ref/SocketConnect])  ▪  (RefLink[SocketOpen, paclet:ref/SocketOpen])  ▪  (RefLink[SocketObject, paclet:ref/SocketObject])  ▪  (RefLink[Read, paclet:ref/Read])  ▪  (RefLink[DeviceReadBuffer, paclet:ref/DeviceReadBuffer])  ▪  (RefLink[ByteArrayToString, paclet:ref/ByteArrayToString])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Network Programming, paclet:guide/NetworkProgramming])}})}})