# NestJS Microservices (TCP Connection Refused)

**URL:** <https://community.fly.io/t/nestjs-microservices-tcp-connection-refused/4871>\
**Category:** General\
**Created:** [April 25, 2022, 9:38pm UTC](https://community.fly.io/t/nestjs-microservices-tcp-connection-refused/4871 "2022-04-25T21:38:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dev2](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Dev2](https://community.fly.io/u/Dev2)\
**Post date:** [April 25, 2022, 9:38pm UTC](https://community.fly.io/t/nestjs-microservices-tcp-connection-refused/4871/1 "2022-04-25T21:38:40Z")

</div>

I’m trying to deploy some microservices writing in Nestjs (NODE) which connect via TCP. I’m using internal docker addresses and default port (8080) for each service. But when I run `fly launch` i got this errors in VM:

````auto
230cd37130e7f9c96f951ed2ad499c8f tcp-8080 critical dial tcp 172.19.12.2:8080: connect: connection refused ```

Then, my deploy failed because health check

````

Description = Failed due to unhealthy allocations - no stable job version to auto revert to  
Instances = 1 desired, 1 placed, 0 healthy, 1 unhealthy

```auto

Can anyone help me ? There's a discord/slack community ?
```

---

<div class="post-metadata">

**Author:** ![greg](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/greg/32/2119_2.png) [@greg](https://community.fly.io/u/greg)\
**Post date:** [April 25, 2022, 9:46pm UTC](https://community.fly.io/t/nestjs-microservices-tcp-connection-refused/4871/2 "2022-04-25T21:46:13Z")

</div>

Can you maybe see some logs to see why it failed? With `fly logs`? Most apps should put _some_ kind of stacktrace/exception/message to say if something failed.

One possible issue with Node is it’s only listening on IPv4, and Fly’s internal network uses IPv6. So make sure your server is listening on ‘::’ e.g it may look something like this:

```auto
server.listen(8080, '::');

```

When you deploy Fly’s system will check the tcp connection to it is working and if not, that would indeed result in the deploy failing:

> **[Fly Launch configuration (fly.toml)](https://fly.io/docs/reference/configuration/#services-tcp_checks)**
>
> Documentation and guides from the team at Fly.io.

There is a grace period option there, so _another_ thought would be if your app takes time to start, you could up that time a bit in your `fly.toml` to e.g `5s`

---

<div class="post-metadata">

**Author:** ![Dev2](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Dev2](https://community.fly.io/u/Dev2)\
**Post date:** [April 25, 2022, 10:21pm UTC](https://community.fly.io/t/nestjs-microservices-tcp-connection-refused/4871/3 "2022-04-25T22:21:08Z")

</div>

My service runs with no errors when i use either `0.0.0.0` or `::` as host value. But how can I connect other service with this one?

I’m trying to use internal fly addr. ex: `lax.service-one.internal` to connect service-two.

---

<div class="post-metadata">

**Author:** ![Dev2](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Dev2](https://community.fly.io/u/Dev2)\
**Post date:** [April 25, 2022, 10:23pm UTC](https://community.fly.io/t/nestjs-microservices-tcp-connection-refused/4871/4 "2022-04-25T22:23:43Z")

</div>

Example:  
User Microservice

 ![image](https://global.discourse-cdn.com/fly1/original/3X/b/2/b27a98fb9a2fc3e305f9c22dbb927cb4b405b43a.png)

It’s runnning on port 8080, in default address.

Then, other service would connect from lax.user-microservice-internal in port 8080. does it ?

---

<div class="post-metadata">

**Author:** ![greg](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/greg/32/2119_2.png) [@greg](https://community.fly.io/u/greg)\
**Post date:** [April 25, 2022, 10:43pm UTC](https://community.fly.io/t/nestjs-microservices-tcp-connection-refused/4871/5 "2022-04-25T22:43:09Z")

</div>

Well I haven’t used Nest, my thoughts were for general Node. But yes, listening on :: like that _looks_ ok to me. It sounds like the healthchecks in your first post must be passing now in order to get that app deployed, else it wouldn’t even get to that point.

As regards connecting to it from _other_ apps, yes, assuming they are in the same organization, you _should_ be able to connect using e.g `http://app-name.internal:8080`. You use http because the connection is already encrypted.

Personally I would experiment with the hostname. Like try using [http://app-name.internal:8080](http://app-name.internal:8080). If _that_ works, next try e.g _region_.app-name.internal. Try and narrow down the failure that way. If e.g the hostname _without_ the region works, but with it does not, that would be one for Fly to investigate.

---

<div class="post-metadata">

**Author:** ![Dev2](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Dev2](https://community.fly.io/u/Dev2)\
**Post date:** [April 25, 2022, 10:47pm UTC](https://community.fly.io/t/nestjs-microservices-tcp-connection-refused/4871/6 "2022-04-25T22:47:17Z")

</div>

You’re right!

I did it just changing listenning hostname to `::` like above. Then i defined envs in fly.toml with the fly internal address.

It works!

Thanks a lot @greg

---

<div class="post-metadata">

**Author:** ![greg](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/greg/32/2119_2.png) [@greg](https://community.fly.io/u/greg)\
**Post date:** [April 25, 2022, 11:02pm UTC](https://community.fly.io/t/nestjs-microservices-tcp-connection-refused/4871/7 "2022-04-25T23:02:44Z")

</div>

You’re welcome 🙂 Glad it worked.
