WebSockets
Interacting with web socket servers from Blueprint
You can connect to your web socket server to send/receive JSON messages from blueprint.
Usage Overview
There are two different types of web socket connections
M2_JsonWebSocketConnection
- can be used for sending and receiving json messagesM2_StringWebSocketConnection
- can be used for sending and receiving string messages(
M2_WebSocketConnection
has been deprecated)
To use one of these web socket connections, create a new instance of the class and store it somewhere in a Blueprint graph.

For example usage, please see BP_WebsocketExample
. This provides a complete example of connecting to, sending and receiving messages using a M2_JsonWebSocketConnection
.
Connecting
To connect to the Web Socket server you call the Connect method on the connection class. This is Connect Json Websocket
, or Connect String Websocket
respectively.

The parameters are as follows:
Url
The web socket URL to connect to. The wss
or ws
protocols must be specified.
This URL must conform to the requirements described in Configuing External URL Access
Yes
Protocol
An optional subprotocol to pass to your server on connection. If not specified, this will default to empty.
No
Authentication
Optional authentication headers, using M2's built-in authentication, used in the same way as Http requests. See Authentication
No
Headers
An optional collection of other headers to pass to your server as part of the handshake. Used in the same way as Http headers. See Headers
No
On Connected
A delegate that is called if the web socket successfully connects. When this event is received, you can send and receive messages.
No, but recommended
On Connection Error
A delegate that is called if the web socket fails to connect. If this is called, the web socket cannot be used until it is connected.
No, but recommended
On Disconnected
A delegate that is called if the web socket disconnects. This is called if the disconnection is user driven or the server connection is broken. If this is called, the web socket cannot be used until it is connected.
No, but recommended
On Message Received
A delegate that is called whenever the web socket receives a message from the server. Messages must be deserializable from a JSON string to be passed to your handler. If you do not provide this delegate, your socket will ignore any messages that are received.
No, but recommended
The outputs are as follows:
Connecting
The connection request was correctly sent. From this point, either OnConnected
or OnConnectionError
will be called.
Invalid Url
The connection request was not sent because the provided Url was rejected.
Url Blocked
The connection request was not sent, because the Url was not allowed, due to the access policy for your organization. See Allowed External URLs
Invalid Headers
The connection request was not sent because the provided headers were rejected.
Failed
Failed for a reason not related to the parameters. If this happens, please check your logs for errors.
Failed Connection In Progress
The current connect call failed because there was already an existing connection in progress (or the websocket has already connected)
Sending Messages
Once your web socket is connected you can send messages via the appropriate "Send" functions:
For
M2_JsonWebSocketConnection
, you haveSendJsonObject
andSendJsonValue
, either of which can be used. These will be sent to your remote server as serialized strings.For
M2_StringWebSocketConnection
, you haveSendMessage
SendJson
. This will take a JsonObject
and send it to your remote server as a serialized string.

M2_JsonWebSocketConnection
Handling Disconnection
Disconnection can be manually requested by calling the Disconnect
event. This will send a default status of 1000
and no reason string.
Your web socket server connection may be disconnected for other means, such as network errors between the client and web socket server, problems in the web socket server or just that the connection is set to time out.
You can call Reconnect
to connect to a web socket that has moved into the disconnected state; however you must have called Connect
on it beforehand.
Last updated
Was this helpful?