Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Scratch Extension Protocol Discussion [New]
- mportier
-
6 posts
Scratch Extension Protocol Discussion [New]
If you were directly connecting the Kinect to Scratch, a polling method would make sense but since it isn't you'll be using another program to get the data from the Kinect, you can just create a block that asks the external program for the value you want and the program could send events when the Kinect is turned on or off. With just polling/HTTP you can't get told when the Kinect is turned off. You wouldn't be able to get “real-time” data.Why? The extension protocol is designed for sensor hardware like the Kinect, where an event-based system makes no sense at all. Normally you use events because you want to get told when the value changes; but the Kinect sensor values are changing constantly, so polling is fine since you know there's going to be fresh data.Polling for results is awful, in my opinion. I also agree that a more polling/event based idea would be much nicer.Just implement raw sockets and a JSON transmission protocol. Much easier and less HTTP parsing required.We had something like that before. We switched to HTTP because it's much easier to parse: nearly every language has built-in HTTP libraries.
Anyway, with sockets (and depending on how an event based system is designed, it could also do it) you get the best of both worlds.
Honestly, from a teaching perspective, I think that the way internally the system works (polling or socket-event-driven) is less interesting then how the events can be processed by the children in scratch. (even in socket-implementation stacks there is probably some poll going on already, who really cares?) We should not focus on the mechanism, but on the fact that extansions cannot introduce “event-blocks”
IMHO even based on the current /polll over HTTP there could be a way to not only report variables, booleans or errors… but to have a real-starter-event-block indicating “If remote signal XYZ occurs”
This could be achieved by allowing to specify report variables of the type “e” event in the block-spec.
“e”, “my_event_name”, “When My Event Occurs”
The /poll implementation should then include a line like this
my_event_name 2014021610234500
where the value is an ever increasing number (logically a timestamp of some sort - indicating the unique numberred occurance of the event)
The handling inside scratch could be that the event is triggered (only) when a new timestamp number is detected that is bigger then the previous one.
A call to /reset_all could bring it back to 0.
Everybody happy, and kids get to listen for events, rather then needing to write their own event/poll/loop ?
- mportier
-
6 posts
Scratch Extension Protocol Discussion [New]
You can load up multiple extensions, but its limited to one extension per port. Can Scratch load up multiple extensions or is it limited to one extension?
This is biting us currently, in the sense that it is forcing us to open new service ports for different functional groups we want to offer.
We van prefix our own signal-blocks from /send/:arg to /function-group-x-send/:msg but there is no way to do that with the /poll.
Therefore, it would be nice if the block-spec would just allow for not as much a port-number, but a straight fullblown URI-prefix
The current “extensionPort”: 2002
could simply be overruled by “extensionBaseURI”: "http://localhost:2002/my/convenient/path/"
after which we get poll requests for this extension block over at http://localhost:2002/my/convenient/path/poll
- blob8108
-
1000+ posts
Scratch Extension Protocol Discussion [New]
Oh, you mean custom hat blocks. I suppose that might be nice. a real-starter-event-block indicating “If remote signal XYZ occurs”
What's wrong with having to open different ports? A URL prefix would make the protocol unnecessarily complicated. This is biting us currently, in the sense that it is forcing us to open new service ports for different functional groups we want to offer.
Last edited by blob8108 (Feb. 16, 2014 16:08:46)
- drmcw
-
1000+ posts
Scratch Extension Protocol Discussion [New]
OK would thisIt's a Flash limitation afaik. Can someone explain the point of the cross domain policy file?
<?xml version=“1.0”?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
</cross-domain-policy>
work? As Flash won't need to connect to anything other than localhost, or can you not bother with a crossdomain file at all?
- nXIII
-
1000+ posts
Scratch Extension Protocol Discussion [New]
That's intentional: one app running on one port serving one extension, i.e., one self-contained set of blocks. If you're trying to have one app serving multiple extensions, you're doing something wrong. This is biting us currently, in the sense that it is forcing us to open new service ports for different functional groups we want to offer.
No, you need to <allow-access-from domain=“*.scratch.mit.edu” />. would thiswork?<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
</cross-domain-policy>
- drmcw
-
1000+ posts
Scratch Extension Protocol Discussion [New]
Ok, thanks, next question then is what is the scratch.mit.edu domain going to send?No, you need to <allow-access-from domain=“*.scratch.mit.edu” />. would thiswork?<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
</cross-domain-policy>
- nXIII
-
1000+ posts
Scratch Extension Protocol Discussion [New]
Read the topic post again. what is the scratch.mit.edu domain going to send?
- drmcw
-
1000+ posts
Scratch Extension Protocol Discussion [New]
Um nope, not seeing it. Sorry but obviously need it in big lettersRead the topic post again. what is the scratch.mit.edu domain going to send?

- nXIII
-
1000+ posts
Scratch Extension Protocol Discussion [New]
Oops, misunderstood your question. What I said up there was for extensions that run in the online editor. Um nope, not seeing it. Sorry but obviously need it in big lettersWhere does scratch.mit.edu enter the equation for an extension?
- drmcw
-
1000+ posts
Scratch Extension Protocol Discussion [New]
So original question still stands then. Can the cross domain access file just be ignored for most purposes as everything will be local?Oops, misunderstood your question. What I said up there was for extensions that run in the online editor. Um nope, not seeing it. Sorry but obviously need it in big lettersWhere does scratch.mit.edu enter the equation for an extension?
- itchy20
-
100+ posts
Scratch Extension Protocol Discussion [New]
@drmcw - I think nXIII answered the question, but to clarify, if you are running on the offline editor, a request for cross domain policy is not sent, so you do not need to implement a handler for it. However, if you want the extension to work in the online environment, you will.
- drmcw
-
1000+ posts
Scratch Extension Protocol Discussion [New]
Thank you for clarifying. @drmcw - I think nXIII answered the question, but to clarify, if you are running on the offline editor, a request for cross domain policy is not sent, so you do not need to implement a handler for it. However, if you want the extension to work in the online environment, you will.
- mportier
-
6 posts
Scratch Extension Protocol Discussion [New]
Oh, you mean custom hat blocks. I suppose that might be nice. a real-starter-event-block indicating “If remote signal XYZ occurs”
Yep. That is the idea.
What's wrong with having to open different ports? A URL prefix would make the protocol unnecessarily complicated. This is biting us currently, in the sense that it is forcing us to open new service ports for different functional groups we want to offer.
Fair enough. I don know about the internals obviously, I was guessing current implementation to assemble localhost and portnumber into a base-URI anyway - so the provided shortcut would be able to skip that and solve my grunge with “all those ports”.
But I mentioned “this is biting us” - that doesn mean we can bite back

Meaning: this is surely something we can handle on our side by indeed managing some additional open ports, no worries.
The hat blocks is harder to get around (since it bites the kids), any chance this gets a chance in some upcoming release?
- Metrology1234
-
7 posts
Scratch Extension Protocol Discussion [New]
I am trying to communicate with offline Scratch2 I get these issues.
1. I don't receive a request for the cross domain file? Scratch just starts off sending /polls every 30ms
2. I am having trouble getting scratch to see the reply pair eg, speed 12. It just receives 0's
3. I would like to use the c# .net HTML listener but it will not pass through the messages and just returns an error to Scratch
For 1 and 2 I have been using Apache server on Windows7
for 3 I have been using VS8
1. I don't receive a request for the cross domain file? Scratch just starts off sending /polls every 30ms
2. I am having trouble getting scratch to see the reply pair eg, speed 12. It just receives 0's
3. I would like to use the c# .net HTML listener but it will not pass through the messages and just returns an error to Scratch
For 1 and 2 I have been using Apache server on Windows7
for 3 I have been using VS8
- blob8108
-
1000+ posts
Scratch Extension Protocol Discussion [New]
I think crossdomain.xml isn't needed for the offline player, only for the online one. You can only use HTTP extensions from the offline player now. Not that supporting it in your helper app hurts. 1. I don't receive a request for the cross domain file? Scratch just starts off sending /polls every 30ms
2. I am having trouble getting scratch to see the reply pair eg, speed 12. It just receives 0'sDid you format the poll response correctly? You can't expect anyone to help if you don't post code.
You might find the wiki article Scratch Extension Protocol (2.0) more (or less?) up-to-date than the topic post, particularly the linked PDF with the draft protocol.
- PullJosh
-
1000+ posts
Scratch Extension Protocol Discussion [New]
This might sound like a noobish question, but…
How do I use JS to get the block's input vales (%s, etc.)?
Edit: Sorry, just realized this was for json extensions. Does anybody here know anyway?
How do I use JS to get the block's input vales (%s, etc.)?
Edit: Sorry, just realized this was for json extensions. Does anybody here know anyway?
Last edited by PullJosh (June 6, 2014 18:02:45)
- SCRAXE
-
72 posts
Scratch Extension Protocol Discussion [New]
How do I use JS to get the block's input vales (%s, etc.)?
Hi - have a look in the source of the official Wedo extension for an example of this process:
http://scratch.mit.edu/scratchr2/static/js/scratch_extensions/wedoExtension.js
As an example, the distance hat needs a threshold distance number from within Scratch, and so is defined like this:
['h', 'when distance %m.lessMore %n', 'whenDistance', '<', 20],
If you look in the matching js function ‘whenDistance’
ext.whenDistance = function(s, dist) { return device!=null && ('<' == s ? (getDistance() < dist) : (getDistance() > dist)); };
you will see this function gets defined with two passed arguments - s and dist
These match the two ‘%m’ and ‘%n’ values that the end user has to set within the Scratch block.
So the answer is Scratch will pass the user values across to the appropriate function whenever it needs to process that function.
This process applies in exactly the same way to %s strings as well as the menus and numbers shown above.
Hope that helps!
- PullJosh
-
1000+ posts
Scratch Extension Protocol Discussion [New]
Okay, thanks so much for the help. I would've never figured that out (I'm a bit new to JS :3)How do I use JS to get the block's input vales (%s, etc.)?
Hi - have a look in the source of the official Wedo extension for an example of this process:
http://scratch.mit.edu/scratchr2/static/js/scratch_extensions/wedoExtension.js
As an example, the distance hat needs a threshold distance number from within Scratch, and so is defined like this:['h', 'when distance %m.lessMore %n', 'whenDistance', '<', 20],
If you look in the matching js function ‘whenDistance’ext.whenDistance = function(s, dist) { return device!=null && ('<' == s ? (getDistance() < dist) : (getDistance() > dist)); };
you will see this function gets defined with two passed arguments - s and dist
These match the two ‘%m’ and ‘%n’ values that the end user has to set within the Scratch block.
So the answer is Scratch will pass the user values across to the appropriate function whenever it needs to process that function.
This process applies in exactly the same way to %s strings as well as the menus and numbers shown above.
Hope that helps!
Would you mind explaining the callback thing? So far, all I understand about it is this:
1) I'm doing it wrong.
2) My block never stops going.
Here's my code for a simple “Alert” block (uses JS's window.alert function):
new (function() {
var ext = this;
// Cleanup function when the extension is unloaded
ext._shutdown = function() {};
// Status reporting code
// Use this to report missing hardware, plugin or unsupported browser
ext._getStatus = function() {
return {status: 2, msg: 'Ready'};
};
// Functions for block with type 'w' will get a callback function as the
// final argument. This should be called to indicate that the block can
// stop waiting.
ext.alert_stuffs = function(alert) {
window.alert(alert);
callback();
};
// Block and block menu descriptions
var descriptor = {
blocks: [
['w', 'Alert %s', 'alert_stuffs'],
],
url: 'http://www.google.com/'
};
// Register the extension
ScratchExtensions.register('Cool helpful things extension', descriptor, ext);
})();
Running callback(); doesn't seem to do anything, and I'm not sure if callback() even exists.

Could you help?
- SCRAXE
-
72 posts
Scratch Extension Protocol Discussion [New]
callback needs to be one of the passed arguments to the function.
ext.alert_stuffs = function(alert, callback) {
You are actually duplicating an example that was posted a few days back, have a look at the example in the posts near the bottom of this thread, you can then see the callback being used:
http://scratch.mit.edu/discuss/topic/40989/
ext.alert_stuffs = function(alert, callback) {
You are actually duplicating an example that was posted a few days back, have a look at the example in the posts near the bottom of this thread, you can then see the callback being used:
http://scratch.mit.edu/discuss/topic/40989/
Last edited by SCRAXE (June 6, 2014 19:50:35)
- PullJosh
-
1000+ posts
Scratch Extension Protocol Discussion [New]
Thanks so much! That really helped a lot. I've got my first working extension now! Yay! callback needs to be one of the passed arguments to the function.
ext.alert_stuffs = function(alert, callback) {
You are actually duplicating an example that was posted a few days back, have a look at the example in the posts near the bottom of this thread, you can then see the callback being used:
http://scratch.mit.edu/discuss/topic/40989/

- Discussion Forums
- » Advanced Topics
-
» Scratch Extension Protocol Discussion [New]