A Flow Socket API example demonstrating how to open a connection and send a test message.
We provide a [Flow JavaScript SDK] (https://github.com/flow-ai/flowai-js), but the following example demonstrates opening a connection and sending a test message using vanilla JavaScript in the browser.
<html>
<body>
<script>
(function () {
// Vanilla JS example
// When executing this script. Check your development console for any messages
// This identifies the channel we want to connect with
var clientId = 'YOUR CLIENTID'
// Global references to our WebSocket and interval
var ws
var keepalive
// This methos is where we send test messages
function runTestMessages() {
console.info('runTestMessages')
var message = {
"type": "message.send",
"payload": {
"threadId": "jane.doe",
"speech": "event attachment",
"attachment": {
"type": "event",
"payload": {
"name": "MY EVENT"
}
},
"originator": {
"name": "Jane Doe",
"role": "external",
"profile": {
"fullName": "Jane Doe",
"firstName": "Jane",
"lastName": "Doe",
"gender": "F",
"locale": "en-US",
"timezone": -5,
"country": "us",
"email": "[email protected]",
"picture": "https://en.wikipedia.org/wiki/File:YellowLabradorLooking_new.jpg"
},
"metadata": {
"clientNumber": "12345",
"preference": "A,B,G"
}
}
}
}
wsSendMessage(message)
}
// Get a new wss endpoint
function getWsEndpoint() {
console.info('Request endpoint')
// socket.info endpoint
var socketInfoUrl = 'https://sdk.flow.ai/socket.info?clientId=' + clientId + '&sessionId=' + Date.now();
// Create a GET request
var req = new XMLHttpRequest();
req.onload = wsEndpointResponse
req.responseType = 'json';
req.open('GET', socketInfoUrl, true);
req.send();
}
// Called when we get a response from socket.info
// validate the response and open a websocket connection
function wsEndpointResponse(e) {
console.info('Received endpoint')
var xhr = e.target;
if(xhr.response.status !== 'ok') {
// This is not OK..
console.error('Error while fetching wss url', xhr.response)
return
}
// Get the endpoint from the response
var endpoint = xhr.response.payload.endpoint
startWebsocket(endpoint)
}
// Open a new websocket connection
function startWebsocket(endpoint) {
console.info('Websocket start connection with endpoint', endpoint)
// Create a new socket
ws = new WebSocket(endpoint);
ws.onopen = wsOnOpen;
ws.onmessage = wsOnMessage;
}
// Handler called when the socket makes a connection
function wsOnOpen(e) {
console.info('Websocket connection open')
// Start the keepalive
wsStartKeepalive()
// Run our test messages
runTestMessages()
}
// Handler called when the socket receives a message
function wsOnMessage(e) {
var json = JSON.parse(e.data)
console.info('Websocket received json', json)
}
// Simple keep alive method (sending pings)
function wsStartKeepalive() {
clearInterval(keepalive)
// Send a ping 30 seconds
keepalive = setInterval(function() {
wsSendMessage({
"type": "ping"
})
}, 30 * 1000)
}
// Helper method for sending messages
function wsSendMessage(message) {
ws.send(JSON.stringify(message))
}
// Start with sending a GET request for a WSS endpoint
getWsEndpoint()
}());
</script>
</body>
</html>