typesense / typesense-dart

Dart client for Typesense
https://typesense.org/docs
Apache License 2.0
71 stars 16 forks source link

What am I doing wrong? Trying to connect locally #138

Closed ember11498 closed 5 months ago

ember11498 commented 8 months ago

I have this code which works:

class MyTypesense with UsersTypesenseQueries {
  @override
  late final Client client;

  MyTypesense() {
    final config = Configuration(
      'xyz',
      nodes: {
        Node.withUri(
          Uri(
            scheme: 'http',
            host: '171.122.255.261',
            port: 8108,
          ),
        ),
      },
      numRetries: 3, // A total of 4 tries (1 original try + 3 retries)
      connectionTimeout: const Duration(seconds: 15),
    );
    client = Client(config);
  }
}

this represents the IP of my droplet: 171.122.255.261 (fake)

So when I try to connect from other machines it works. The problem is when I try to connect from inside my droplet. My droplet is ubuntu with docker installed with typesense container running and healthy.

Inside my droplet if I try:

class TypesenseDatabase {
  late final Client client;

  TypesenseDatabase() {
    final config = Configuration(
      'xyz',
      nodes: {
        Node.withUri(
          Uri(
            scheme: 'http',
            host: '127.0.0.1',
            port: 8108,
          ),
        ),
      },
      numRetries: 3, // A total of 4 tries (1 original try + 3 retries)
      connectionTimeout: const Duration(seconds: 2),
    );
    client = Client(config);
  }
}

But this does not work. I have no idea why. I got this log:

"ClientException with SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = 127.0.0.1, port = 44250, uri=http://127.0.0.1:8108/collections?"

I dont understand why port = 44250. but everytime I try to run it the port changes. Per example here is another log after I run it again:

"ClientException with SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = 127.0.0.1, port = 44408, uri=http://127.0.0.1:8108/collections?"

What am I missing?

My typesense container is mapped to port 8108:8108 I believe the error here is that typesense assings a random port instead of using the port 8108

happy-san commented 8 months ago

Hi @ember11498 Could you try InternetAddress.loopbackIPv4.address as the host value?

ember11498 commented 8 months ago

Hi @ember11498 Could you try InternetAddress.loopbackIPv4.address as the host value?

I just tried it and it returns 127.0.0.1 and throws the same error

Typesense is installed via docker it's not directly in the droplet

if i write curl http://127.0.0.1:8108/health or curl http://localhost:8108/health or curl http://171.122.255.261/health in the command line i get

{"ok":true}

Also, if I use 171.122.255.261 (the public ip address of my droplet) it doesnt work either when I call it from inside the droplet. I can only manage to use typesense correctly when I connect via other machines

ember11498 commented 8 months ago

not sure if this helps:

root@test:~# ip addr show lo 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever

kishorenc commented 7 months ago

I think you might get better help on a Docker specific forum / Stackoverflow. Unfortunately we are not familiar enough with Docker networking configuration to help out here.

ember11498 commented 7 months ago

@kishorenc I just asked in stackoverflow. but strange behavior! By the way, a video tutorial on common search queries on youtube would be good and I am sure you guys can easily do it

ember11498 commented 7 months ago

@kishorenc UPDATE: I solved my problem. When working with typesense container, if you want to connect locally you cannot use localhost or 127.0.0.1. You have to use the container's IP, in my case it was 172.18.0.6. With that IP its now working properly.

You can check the IP of the typesense container using this in the command line (linux):

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' typesense_container_name