Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Get Cloud Data Value using Javascript
- LankyBox01
-
1000+ posts
Get Cloud Data Value using Javascript
I need to get the value of a certain cloud data variable using javascript. How do i do this?
- Chiroyce
-
1000+ posts
Get Cloud Data Value using Javascript
Certain value? Well you need the sessionID, then do a handshake with wss://clouddata.scratch.mit.edu with the sessionID, the data is
Here is an example.
Then the response will be like this
Here is an example.
You might get many responses, depending upon the number of Cloud Vars.
{"method":"handshake","user":"<username>","project_id":"<project_ID>"}
Here is an example.
{"method":"handshake","user":"Chiroyce","project_id":"488029945"}
Then the response will be like this
{"method":"set","project_id":"<project_id>","name":"☁ <var_name>","value":"<value>"}
Here is an example.
{"method":"set","project_id":"488029945","name":"☁ Cloud","value":"000"}
You might get many responses, depending upon the number of Cloud Vars.
Last edited by Chiroyce (May 1, 2021 12:16:06)
- LankyBox01
-
1000+ posts
Get Cloud Data Value using Javascript
wss://clouddata.scratch.mit.edu with the sessionID, the data isWhat? Certain value? Well you need the sessionID, then do a handshake with{"method":"handshake","user":"<username>","project_id":"<project_ID>"}
Here is an example.{"method":"handshake","user":"Chiroyce","project_id":"488029945"}
Then the response will be like this{"method":"set","project_id":"<project_id>","name":"☁ <var_name>","value":"<value>"}
Here is an example.{"method":"set","project_id":"488029945","name":"☁ Cloud","value":"000"}
You might get many responses, depending upon the number of Cloud Vars.
Explain.
I don't understand.
I'm new to js
- 9gr
-
1000+ posts
Get Cloud Data Value using Javascript
if you dont want to use websockets, use
GET https://mv-ezproxy-com.ezproxyberklee.flo.org/logs?projectid=<PROJECT ID>&offset=0&limit=<LOG LIMIT>
GET https://mv-ezproxy-com.ezproxyberklee.flo.org/logs?projectid=<PROJECT ID>&offset=0&limit=<LOG LIMIT>
var projectid = 0; // replace 0 with your projectid fetch(`https://clouddata.scratch.mit.edu/logs?projectid=${projectid}&offset=0&limit=40`) .then(res => res.json()) .then(res => { console.log(res[0]) // should log the latest cloud variable commit in the console })
Last edited by 9gr (May 1, 2021 12:33:39)
- LankyBox01
-
1000+ posts
Get Cloud Data Value using Javascript
Thanks, it works perfectly. if you dont want to use websockets, use
GET https://mv-ezproxy-com.ezproxyberklee.flo.org/logs?projectid=<PROJECT ID>&offset=0&limit=<LOG LIMIT>
-snip-
But how do i get the values from the returned string? Like “user” or “name” or “value”.
Edit: Nevermind, i just add .user at the end of res.
Last edited by LankyBox01 (May 1, 2021 12:45:40)
- Chiroyce
-
1000+ posts
Get Cloud Data Value using Javascript
basically it gets the latest value of all cloud variables, but @9gr's only gets the last variable that was set. What?
Explain.
I don't understand.
I'm new to js
For example there are 2 cloud vars, “a” and “b”
A is set to 99
b is set to 33
if you run 9gr's code, you'll only get b's value, and not a as it was set before b. But if you use the websockets way, then you will get the values of a and b.
const socket = new WebSocket("wss://clouddata.scratch.mit.edu/") socket.addEventListener("open", () => { socket.send(`${JSON.stringify({name: "handshake", user: Chiroyce, project_id: 493371185})}\n`) })
Last edited by Chiroyce (May 1, 2021 12:51:57)
- LankyBox01
-
1000+ posts
Get Cloud Data Value using Javascript
My username is not defined. But i only have one cloud variable in the project so 9gr's code is fine. Thanksbasically it gets the latest value of all cloud variables, but @9gr's only gets the last variable that was set. What?
Explain.
I don't understand.
I'm new to js
For example there are 2 cloud vars, “a” and “b”
A is set to 99
b is set to 33
if you run 9gr's code, you'll only get b's value, and not a as it was set before b. But if you use the websockets way, then you will get the values of a and b.Replace “Chiroyce” with your username, replace “493371185” with your project id, this is better but it'll only show it in the console. I got the code from @uwv in this forum post.const socket = new WebSocket("wss://clouddata.scratch.mit.edu/") socket.addEventListener("open", () => { socket.send(`${JSON.stringify({name: "handshake", user: Chiroyce, project_id: 493371185})}\n`) })
- Discussion Forums
- » Advanced Topics
-
» Get Cloud Data Value using Javascript