Dart pass TCP ServerSocket client to Isolate

Issue

On Quora Seth Ladd once said:

Consider the possibility of accepting new connections and giving them to isolates for doing the work. As of today, you can pass sockets to other isolates (by reference) and scale up linearly.

I tried to accomplish that in many ways and each of them failed.
My code is:

ServerSocket.bind("127.0.0.1", 5555).then((ServerSocket socket) {
    socket.listen((client) {
        Isolate.spawn(SomeClient.start, client);
    });
});

Which each time throws the same exception.

Illegal argument(s): Illegal argument in isolate message : (object extends NativeWrapper)

Getting a stream from socket gives the same result. Has anyone any idea how can I accomplish what has Seth said?

Solution

The only thing I found is the opposite of what I thought should work

https://groups.google.com/a/dartlang.org/forum/#!topic/misc/G9wYnvSG0UQ

Here the ServerSocket is passed to other isolates and the incoming connections are distributed equally to the isolates that listen on that port.

In the discussion is also mentioned that it is not supported to pass connections but only ServerSockets and passing connections is not a near term goal.

Answered By – Günter Zöchbauer

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *