Skip to content

Introduction to Distributed Programming in NetsBlox

Brian Broll edited this page Jul 20, 2021 · 13 revisions

Note: This documentation is now self-hosted! Documentation for the latest release can be found here.

NetsBlox provides a few main abstractions to enable users to create distributed applications: Remote Procedure Calls, the Room and Messaging.

Remote Procedure Calls (Services)

Services are provided by the NetsBlox server and allow the client to easily incorporate third party utilities (such as Google Maps, MovieDB, Weather, and Traffic data) as well as simplifies the creation of games which otherwise may be more complex (such as Battleship and ConnectN).

Using services is pretty easy; simply select the "Services" tab from the project menu:

Services

Then select the desired Service to import from the dropdown:

Select Map Service

Now, the map rpc blocks are available under the "Custom" tab:

Map Blocks under "custom" tab

Then you can use the provided blocks to leverage the NetsBlox service:

Setting stage background to map

Room

The NetsBlox Room defines the local network for a given application. A room can contain a number of different roles which are distinct NetsBlox clients running in their own window or machine. For example, a TicTacToe game may consist of two roles, X and Y, which represent the two players in the game of TicTacToe:

TicTacToe Room

Room Management

The owner of the TicTacToe game can use the room tab to add new roles to the room (using the "plus" button), add/remove roles, rename roles and invite other users to occupy different roles in the given room. This is done by clicking on the given role in the room tab:

Edit Role

From this dialog, there are a number of supported actions:

  • cloning the role: This will create a new role in the room which is a copy of the clicked role. This includes sprites, blocks, costumes, code blocks, etc.
  • moving to the role: This option allows the owner to move from the currently occupied role to the clicked role.
  • inviting another user: Inviting a user will allow the owner to invite other users to occupy the given role at the room. This allows the given user to run and edit the blocks running at the given role.
  • deleting the role: This will remove the role and all it's blocks from the given room and project.

However, if a user is already occupying the given role, then the dialog also allows the owner to evict the user. This will remove the user from the owner's project:

Evict User Dialog

Roles can be renamed by clicking on the name of the role:

Rename Role

Messaging

Remote messaging allows users to send messages over the internet between different instances of NetsBlox, enabling users to create distributed, online apps. Unlike events, messages can contain a structured data payload. That is, they can be used to send information between NetsBlox scripts and users. The structure of messages is defined in a message type. A message type contains the name of the message as well as the names of the expected fields of the message. For example, a message type may specify a type of message called "TicTacToe" that contains "role", "row" and "column" fields.

Message sending and receiving can be done using the following blocks:

Messaging Blocks

The top block (starting with when I receive) is used to receive messages from others and the bottom block (starting with send msg) can be used to send messages to other users.

Receiving Messages

The message handling block contains a dropdown for setting the message type for which it listens:

Setting message type for message hat

This dropdown contains the names of all defined message types in the given role. Selecting one of the message types will dynamically change the block and add variable blocks for each of the expected fields in the given message type:

Message hat for TicTacToe

Upon receiving a message, these variable blocks will be set to the values received in the given message. In the above example, this allows the user to retrieve the values sent for role, row and column.

Sending Messages

Like the message handling block, the send msg block also contains a field for specifying the message type:

Setting the message type in send msg block

After setting the message type, the send block will update to provide inputs for each of the expected inputs:

TicTacToe send message block

The empty input fields contain grey-ed out text displaying the field name for the corresponding input. These inputs can be filled just as any other block input slot:

TicTacToe send msg block with content

Finally, the block only needs to specify to whom we want to send the given message. This is specified using the last dropdown:

Set the target role

This dropdown contains all the roles in the room as well as two extra fields: "others in room" and "everyone in room". Setting the field to "others in room" will result in the given role broadcasting the message to all other roles in the room whereas "everyone in room" will send the message to everyone in the room including the sender. Selecting any other entry will result in the message being sent only to the corresponding role.

After selecting the role, the message can be sent! In the following example, the block will send a TicTacToe message with the role field set to the role name of the sender, the row field set to 3 and the column field set to 4. This message will be broadcasted to everyone else in the room (not including the sender).

send TTT msg to everyone else

Custom Message Types

A number of the Services include predefined message types to be used with the given service (such as the TicTacToe, Earthquakes and Battleship services). However, there are many cases in which a user may want to define a custom message type. This can be done by first clicking on the "Make a message type" block in the block palette under the "Services" tab:

Services Tab

This will open the message type creation dialog. This dialog provides an interface for specifying the name and fields for the given message type:

Message Type Dialog

After selecting "OK", the given message type will be created! An example of creating the TicTacToe message type used throughout this example is shown below:

TicTacToe Message Type Def

In this example, the name has been set to "TicTacToe" and the fields input has been extended to have three slots (by clicking the right arrow). These fields have been named "role", "row" and "column".