achilleasa / dart_amqp

Dart AMQP client implementing protocol version 0.9.1
MIT License
79 stars 40 forks source link

Sharing connection across isolates #113

Open thanh-cao opened 10 months ago

thanh-cao commented 10 months ago

Hello. I'm building an app which allows a core device exchanging messages to several children devices at the same time. So what I have built is a set up of isolates which handling communicating each of the children devices separately through RabbitMQ. The idea is that the main isolate will create a connection to RabbitMQ and then isolates should be able to grab this connection to respectively create channel, and subsequently queues, to send and receive messages to the children devices. However, I cannot send the Client object to isolates due to "Invalid argument(s): Illegal argument in isolate message: object is unsendable" due to RestartableTimer in _ClientImpl. I have tried also to send Channel, Queue, and Consumer object instead but it is also not possible due to Completer in _ChannelImpl. Is there a way to achieve such isolating operations under one connection?

rajmaniar commented 10 months ago

This is an inherent limitation of Dart's isolate system -- you can only send simple objects (e.g. maps, objects with primitive properties, etc) over a SendPort. You could pass a serializable object between isolates (MsgPack, Protobufs, Json, etc) but, depending on your use case, you might find it better to open a rabbit client+channel in each isolate. Good luck!