Skip to main content
Version: 🚧 Alpha 🚧

Using the Network Skill

The network skill connects GAMA agents to other agents or network services. It provides connection and group actions and uses the messaging actions send, has_more_message and fetch_message to exchange data.

Choose a protocol​

The connect action supports these protocol values:

ProtocolConnection role
tcp_server, tcp_clientCreate a TCP server or connect as a client.
websocket_server, websocket_clientCreate a WebSocket server or connect as a client.
udp_server, udp_emitterReceive UDP packets or send them to a UDP server.
MQTTConnect to an MQTT broker. This is the default when no protocol is specified.
httpSend HTTP requests to a web service.
arduinoConnect through the Arduino connector.

The server or broker required by the chosen protocol must be available before clients connect. HTTP uses a web service as the endpoint; Arduino connections require a compatible device and setup. See the bundled Plugin models / Network examples for protocol-specific configuration.

Connect an agent​

Attach network to the species that will communicate. For example, this client connects to a TCP server listening on port 9876:

species NetworkClient skills: [network] {
init {
do connect(
to: 'localhost',
protocol: 'tcp_client',
port: 9876,
with_name: name
);
}
}

For a TCP server, use protocol: 'tcp_server' with the same host and port. connect returns true when the connection succeeds. When it fails, GAMA reports a connection error and returns false; inside a try/catch, the failure is thrown to the catch block.

Send and receive messages​

Send a message to a connected agent by its network name. In this example, the other agent connects with the name Server. Poll the mailbox in a reflex and fetch each available message:

species NetworkClient skills: [network] {
reflex send_update when: time = 1 {
do send(to: 'Server', contents: 'Hello from GAMA');
}

reflex receive_messages when: has_more_message() {
loop while: has_more_message() {
message received <- fetch_message();
write string(received.contents);
}
}
}

fetch_message removes and returns the first message in the mailbox. The mailbox variable can also be read directly. The messaging actions are processed during the simulation cycle; fetch_message_from_network forces an immediate network refresh and is intended for specific cases.

The send action accepts a GAML value as its contents. What can be exchanged with a non-GAMA application depends on the selected connector and the format expected by that application. For HTTP, the bundled GET and POST models show how to set up the request and read the response.

Send to a group​

An agent can join a named group and send a message to that group. Members that have joined the group can receive the broadcast:

do join_group(with_name: 'observers');
do send(to: 'observers', contents: 'Simulation update');

Use leave_group(with_name: 'observers') when an agent should stop receiving messages from the group. The built-in group ALL contains all agents connected through the network skill.

Call disconnect to close the connections of an agent. For protocol guidance and additional examples, see the network guide on the GAMA site. For the complete attribute and action syntax, see the network skill reference.