SocketConnect[address]
makes a socket connection at the specified address and returns a SocketObject.
SocketConnect[address,"protocol"]
makes a connection to the host at address with the specified protocol.
SocketConnect[socket]
makes a connection to a local socket opened in the current session.
SocketConnect
SocketConnect[address]
makes a socket connection at the specified address and returns a SocketObject.
SocketConnect[address,"protocol"]
makes a connection to the host at address with the specified protocol.
SocketConnect[socket]
makes a connection to a local socket opened in the current session.
Details
- SocketConnect makes a connection at a specified address using one of three types of protocols: TCP, ZMQ or WebSocket.
- Possible socket addresses include:
-
{host,port} hostname or IP address and port number {host,"service"} hostname or IP address and service name "host:port" hostname or IP address with port port local socket localhost:port URL["url"] or "url" complete URL (e.g. http://www.wolfram.com) - Service names are used to determine the port to connect to. The operating system's service name database is used for this. The service name can also be "wss" or "ws", corresponding to web socket connections.
- Hostnames have forms like "www.wolfram.com".
- Hostnames in the form of IP addresses can be given as "140.177.0.0" or IPAddress[string], where string is an IPv4 or IPv6 IP address.
- When the address is given as a URL with no port number specified, the port number is deduced from the URL scheme (e.g. port 80 for HTTP).
- SocketConnect[SocketObject[…]] can be used to connect to a local socket opened by SocketOpen.
- Supported protocols include:
-
"TCP" raw TCP stream protocol "ZMQ" ZeroMQ one-to-one message protocol {"ZMQ","type"} ZeroMQ protocol of the specified type "WSS" secure WebSocket protocol "WS" WebSocket protocol - If the WebSocket protocol is used, address can be an HTTPRequest object.
- If the URL includes a WebSocket scheme ("wss://" or "ws://") then SocketConnect will select the WebSocket protocol.
- For ZMQ sockets, possible types include:
-
"Pair" socket that can send/receive with exactly one client peer "Publish" socket that sends messages out to all connected clients "Subscribe" socket that receives messages from a publisher socket "Request" socket to send request messages to a corresponding reply socket and receive response messages "Reply" socket to receive request messages from a corresponding "Request" socket and send response messages back "Dealer" same as "Request" socket except sent messages are delivered in round-robin style to all connected clients "Router" same as "Reply" socket except messages are received in fair-queueing style from all connected clients and maintain routing information "Pull" socket for pipeline topologies to receive messages "Push" socket for pipeline topologies to send messages "XPublish" same as "Publish", but received messages on this socket are subscriptions "XSubscribe" same as "Subscribe", but messages sent over this socket are used as subscriptions to the corresponding "XPublish" socket "Stream" socket for connecting a ZMQ socket to non-ZMQ TCP sockets - With no ZMQ protocol type specified, the underlying socket is a "Pair" socket.
- Close or DeleteObject can be used to close a connection opened by SocketConnect.
Examples
open all close allBasic Examples (3)
Open a connection to a server specified by a URL:
socket = SocketConnect["http://exampledata.wolfram.com"]Write a basic HTTP request to the socket:
WriteString[socket, "GET /50states.txt HTTP/1.0
"]Read the entire response as a string:
result = ByteArrayToString[SocketReadMessage[socket]];Display the result, without extra newlines:
Snippet[result]Close[socket]server = SocketOpen[Automatic, "ZMQ"]client = SocketConnect[server]Send a string to the server socket:
WriteString[server, "hello world"]Read the string from the client socket:
message = SocketReadMessage[client]Convert the ByteArray to a string:
ByteArrayToString[message]Close /@ {server, client}Open a web socket to a server that echoes incoming data:
echoServer = SocketConnect["wss://echo.websocket.org"]Read the server's first message:
SocketReadMessage[echoServer]Send a message and receive the server's echo:
SocketWriteMessage[echoServer, StringToByteArray["test string"]];
message = SocketReadMessage[echoServer];
ByteArrayToString[message["DataByteArray"]]Scope (1)
Open a ZMQ socket on an available port:
server = SocketOpen[Automatic, "ZMQ"]Connect to the socket, manually specifying the port:
ipaddress = server["DestinationIPAddress"]
port = server["DestinationPort"]client = SocketConnect[{ipaddress, port}, "ZMQ"]Send any expression to the server socket:
Write[server, Graphics[{Orange, Disk[]}]]Read the expression from the client socket:
Read[client]Send a list of expressions to the server socket:
Do[Write[server, Graphics[{RandomColor[], Disk[]}]], 10]Read the expressions from the client socket:
ReadList[client, Expression, 10]Close /@ {server, client}Applications (1)
This example demonstrates the use of a ZMQ publish-subscribe model where a single server broadcasts messages and multiple clients receive these messages. For simplicity, this example runs both the publisher and the multiple subscribers in the same Wolfram Engine process. Typically, the publisher and the subscribers are running in different processes and/or on different machines.
Open a socket for the publisher:
publisher = SocketOpen["tcp://127.0.0.1:5858", {"ZMQ", "PUB"}]Connect multiple subscribers to the publisher:
subscribers = Table[SocketConnect["tcp://127.0.0.1:5858", {"ZMQ", "SUB"}], 4];Ensure that all subscribers are listening to all messages:
Scan[SetOptions[#, "ZMQ_SUBSCRIBE" -> All]&, subscribers]No messages have been sent yet, so the subscribers are not ready to read anything:
SocketReadyQ /@ subscribersSend a message from the publisher:
WriteString[publisher, "hello"]Now the subscribers are ready to read the message:
SocketReadyQ /@ subscribersRead the message with all the subscribers:
messages = SocketReadMessage /@ subscribersConvert the resulting byte arrays into strings:
ByteArrayToString /@ messagesClose the publisher and the subscriber sockets:
Close[publisher];
Scan[Close, subscribers];Possible Issues (3)
ReadString only works on TCP sockets, as ReadString will wait until the socket is closed by the server before returning the data. ZMQ sockets are never closed by the server and so thus block indefinitely.
Open a ZMQ socket and connect a client:
server = SocketOpen[Automatic, "ZMQ"];
client = SocketConnect[server];Write a message to the server and close it:
WriteString[server, "hello"];
Close[server];ReadString now will block indefinitely:
TimeConstrained[ReadString[client], 5]Close[client]When using TCP server sockets opened with SocketOpen, the connection must be closed before ReadString will return.
Open a TCP socket and connect a client:
server = SocketOpen[Automatic, "TCP"];
client = SocketConnect[server];Write a message to the remote client and close the server:
remoteClient = First[server["ConnectedClients"]];
WriteString[remoteClient, "hello"];ReadString now will block indefinitely as the socket hasn't been closed yet:
TimeConstrained[ReadString[client], 5]Closing the remote socket will force ReadString to return:
Close[remoteClient];
ReadString[client]Close /@ {client, server}When using an HTTPRequest, it must be well formed and appropriate for establishing web socket connections:
SocketConnect[HTTPRequest["wss://echo.websocket.org", <|"Method" -> "POST"|>]]Non–web socket service names cannot be used with web socket connections:
SocketConnect["ftp://echo.websocket.org", "WSS"]Web socket connection attempts can fail:
SocketConnect["wss://google.com"]Related Guides
Related Links
Text
Wolfram Research (2015), SocketConnect, Wolfram Language function, https://reference.wolfram.com/language/ref/SocketConnect.html (updated 2026).
CMS
Wolfram Language. 2015. "SocketConnect." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2026. https://reference.wolfram.com/language/ref/SocketConnect.html.
APA
Wolfram Language. (2015). SocketConnect. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SocketConnect.html
BibTeX
@misc{reference.wolfram_2026_socketconnect, author="Wolfram Research", title="{SocketConnect}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/SocketConnect.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_socketconnect, organization={Wolfram Research}, title={SocketConnect}, year={2026}, url={https://reference.wolfram.com/language/ref/SocketConnect.html}, note=[Accessed: 12-June-2026]}