From 4eeabfd7d4684ac8a5935e0340f9384edf62c6e7 Mon Sep 17 00:00:00 2001 From: KishanVaishnani <60768270+KishanVaishnani@users.noreply.github.com> Date: Tue, 3 Dec 2024 11:10:24 +0530 Subject: [PATCH] Code example added for streaming custom function --- docs/excel/custom-functions-web-reqs.md | 28 +++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/docs/excel/custom-functions-web-reqs.md b/docs/excel/custom-functions-web-reqs.md index 5a6fc658fd..928cd50bc3 100644 --- a/docs/excel/custom-functions-web-reqs.md +++ b/docs/excel/custom-functions-web-reqs.md @@ -158,18 +158,34 @@ Within a custom function, you can use [WebSockets](https://developer.mozilla.org ### WebSockets example -The following code sample establishes a WebSocket connection and then logs each incoming message from the server. +The following code sample establishes a WebSocket connection and then logs each incoming message from the server to excel reference cell ```js -let ws = new WebSocket('wss://bundles.office.com'); +// functions.js file -ws.onmessage(message) { - console.log(`Received: ${message}`); +var ws = null; + +createSocketObject(invocationData){ + + if(!!ws){ + ws = new WebSocket('wss://bundles.office.com'); + } + + ws.onmessage(message) { + console.log(`Received: ${message}`); + invocationData.setResult(message); // <--- print data in excel cell reference + } + + ws.onerror(error){ + console.err(`Failed: ${error}`); + } } -ws.onerror(error){ - console.err(`Failed: ${error}`); +function getLivePrice(quote_symbol, invocation) { + createSocketObject(invocation); } + +CustomFunctions.associate('quote', getLivePrice); ``` ## Next steps