Flutter RSocket client not connecting to RSocket API created using Spring Boot

526 views Asked by At

I'm playing with RSocket in Flutter. I've created two RSocket apis like below:

    @MessageMapping("stream")
    Flux<Long> numbers(){
        return Flux.interval(Duration.ofSeconds(1));
    }

    @MessageMapping("echo")
    Mono<String> echo(String text){
        return Mono.just(text);
    }

and trying to connect and get answer from them like:

  var rsocket = await RSocketConnector.create().connect('tcp://127.0.0.1:7000');
  var result = await rsocket.requestResponse!(
      (Payload.fromText("message/x.rsocket.routing.v0: 4echo", "effd")));
  print(result.getDataUtf8());

  RSocketConnector.create()
      .setupPayload(
          Payload.fromText("message/x.rsocket.routing.v0: 6stream", ""))
      .connect('tcp://127.0.0.1:7000')
      .asStream()
      .forEach((element) {
    print(element);
  });

. I can call my backend using RSocket command line tool successfully like rsc --route stream tcp://localhost:7000 --stream and it works well. However, Flutter client fails with the error below: Unhandled Exception: SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = 127.0.0.1, port = 41582

any ideas?

1

There are 1 answers

2
Mahdi Amini On BEST ANSWER

thanks to @Richard Head 's point. I got it to work for now by using the IP address that server gets from it's physical network interface. Also, to be able to set the correct route:

Payload routeAndDataPayload(String route, String data) {
  var compositeMetadata =
      CompositeMetadata.fromEntries([RoutingMetadata(route, List.empty())]);
  var metadataBytes = compositeMetadata.toUint8Array();
  var dataBytes = Uint8List.fromList(utf8.encode(data));
  return Payload.from(metadataBytes, dataBytes);
}

  RSocketConnector.create()
      .connect('tcp://192.168.1.196:7000')
      .asStream()
      .asyncExpand((rSocket) =>
          rSocket.requestStream!(routeAndDataPayload("stream", "")))
      .forEach((element) => print(element!.getDataUtf8()));

I gonna PR the Flutter RSocket for an easier way of creating routed payloads.