Can a websocket connection be made in Google app scripts? I wasn't able to find anything about these in Google's documentation, so I am unsure.
The answer is (sadly) still NO.
However, you can star this feature on apps script's issue tracker here - https://issuetracker.google.com/issues/117437427
You can however poll server-side functions using client-side asynchronous JavaScript API using google.script.run
. More on it could be found here - https://developers.google.com/apps-script/guides/html/reference/run
Just adding to Sourabh's answer, although you can't directly use websocket through Google Scripts, you can use it with Javascript.
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
In order to use this, you need to create an html in which you can create your websocket.
https://developers.google.com/apps-script/guides/html/
For example:
<!DOCTYPE html>
<html>
<head>
<title>WebSocket demo</title>
</head>
<body>
<script>
let ws = new WebSocket("ws:port");
ws.onopen = () => ws.send("some message to send); // Sending a message
ws.onmessage = (event) => google.script.run.some_function(event.data); // Receiving a message
</script>
</body>
</html>
So you could either make a web app with google or just an html window with google scripts and embed the websocket in it.
Websocket is a stateful connection. It is obvious that Apps Script is stateless. You can understand it as a virtual machine that is destroyed after execution, so it cannot use websocket. However, you can use HtmlService to create a piece of html code, and put it into the sidebar when the sheet is onOpen. In html, you can use js to complete the websocket link. While exchanging, you can interact with the script of the sheet to transfer data.
© 2022 - 2024 — McMap. All rights reserved.
doGet()
ordoPost()
function, but I don't know of anyway to keep an open connection after the first request. – Galvin