Google app scripts and websocket?
Asked Answered
R

3

12

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.

Rento answered 28/12, 2016 at 23:8 Comment(3)
Duplicate of Google Spreadsheet with data from WebSocket and the answer is No.Septa
What are you trying to accomplish? Are you trying to connect a non-Google website to Apps Script and get a return from the server? Are you planning on using an Apps Script Web App, and connect to a different server than Google? I have seen information about creating your own websocket server using a library, but I don't know how or if that is possible in an Apps Script project. And Apps Script script can detect and respond to either an HTTPS GET or POST request, with a doGet() or doPost() function, but I don't know of anyway to keep an open connection after the first request.Galvin
You could probably try to implement your WebSocket communication in the code that calls the App Scripts API. For example in an Add-On you would do your WebSocket communication in some javascript code of your sidebar and then pass the data as parameter to your App Script functions.Jointed
P
7

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

Plante answered 27/10, 2019 at 18:45 Comment(2)
Could you elaborate how that would help?Nierman
It took Google 5 years to reply on that thread that they won't add it...Sybyl
I
5

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.

Intransigeance answered 27/4, 2021 at 19:42 Comment(0)
H
0

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.

Hag answered 11/11, 2022 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.