Dart:  Concurrency | Isolates event handling

Dart: Concurrency | Isolates event handling

ยท

2 min read

Introduction: In the realm of Dart's concurrency, isolates play a crucial role in executing tasks concurrently. As we continue our exploration, let's delve into the fascinating world of isolate event handling. In this blog post, we'll unravel how isolates can communicate and share information through events, enabling seamless coordination in concurrent applications.

Basic Event Handling in Isolates: Dart isolates communicate with each other using a message-passing mechanism. This allows isolates to share data and coordinate their activities. Events, in this context, are messages sent between isolates to trigger specific actions.

Example: Basic Event Handling

import 'dart:isolate';

void main() async {
  // Spawning two isolates
  Isolate.spawn(isolate1, 'Isolate 1');
  Isolate.spawn(isolate2, 'Isolate 2');
}

void isolate1(String name) {
  print('$name: Started');

  // Sending an event/message to isolate2
  sendEvent('Hello from Isolate 1', 'Isolate 2');
}

void isolate2(String name) {
  print('$name: Started');

  // Receiving and handling events/messages
  receiveEvent();
}

void sendEvent(String message, String recipient) async {
  // Creating a SendPort for communication
  final receivePort = ReceivePort();
  final sendPort = receivePort.sendPort;

  // Spawning an isolate and passing the SendPort
  await Isolate.spawn(eventReceiver, sendPort);

  // Sending the message to the specified isolate
  sendPort.send(message);

  // Closing the ReceivePort
  receivePort.close();
}

void eventReceiver(SendPort sendPort) {
  // Receiving and handling the message
  sendPort.listen((message) {
    print('Received message: $message');
  });
}

Explanation:

  1. isolate1 and isolate2 represent two concurrently running isolates.

  2. sendEvent function is responsible for creating a SendPort, spawning a new isolate (eventReceiver), and sending a message to it.

  3. eventReceiver listens for messages and handles them.

Advanced Event Handling: StreamController For more complex scenarios, Dart provides the StreamController class, allowing isolates to communicate through streams, providing a powerful and flexible event handling mechanism.

Example: Advanced Event Handling with StreamController

import 'dart:async';
import 'dart:isolate';

void main() async {
  // Spawning two isolates
  Isolate.spawn(isolate1, 'Isolate 1');
  Isolate.spawn(isolate2, 'Isolate 2');
}

void isolate1(String name) {
  print('$name: Started');

  // Creating a StreamController for communication
  final controller = StreamController<String>();

  // Sending an event/message to isolate2
  controller.sink.add('Hello from Isolate 1');

  // Closing the StreamController
  controller.close();
}

void isolate2(String name) {
  print('$name: Started');

  // Creating a StreamController for communication
  final controller = StreamController<String>();

  // Receiving and handling events/messages
  controller.stream.listen((message) {
    print('Received message: $message');
  });

  // Closing the StreamController
  controller.close();
}

Did you find this article valuable?

Support Vinit Mepani by becoming a sponsor. Any amount is appreciated!

ย