Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example is not working #682

Closed
matteoventuri7 opened this issue Nov 26, 2024 · 3 comments · Fixed by #683
Closed

Example is not working #682

matteoventuri7 opened this issue Nov 26, 2024 · 3 comments · Fixed by #683

Comments

@matteoventuri7
Copy link

matteoventuri7 commented Nov 26, 2024

Observed behavior

The example reported from Quick Start section inhttps://github.com/nats-io/nats.net is not working.
Error: NATS.Client.Core.NatsException: 'can not connect uris: nats://localhost:4222'

Expected behavior

The example works.

Server and client version

NATS.Net 2.5.4
.Net 8

Host environment

Visual Studio 2022 17.11.6
Windows 11

Steps to reproduce

Create a .Net 8 Console App.
Execute the command dotnet add package NATS.Net
Copy this code:

// NATS core M:N messaging example
using NATS.Net;

await using var nc = new NatsClient();

// Subscribe on one terminal
await foreach (var msg in nc.SubscribeAsync<string>(subject: "foo"))
{
    Console.WriteLine($"Received: {msg.Data}");
}

// Start publishing to the same subject on a second terminal
await nc.PublishAsync(subject: "foo", data: "Hello, World!");

Run

@mtmk
Copy link
Collaborator

mtmk commented Nov 26, 2024

you need to run a NATS Server locally or use the demo server demo.nats.io is you are not behind a firewall.

await using var nc = new NatsClient("demo.nats.io");

Also not in the example above you need to create two console apps one for publishing and one for subscribing or place the subscription in a task and don't exist the program.

Here is another example that should work out of the box unless outbound TCP port 4222 is blocked by a firewall:

// dotnet add package NATS.Net
using NATS.Net;

await using var nc = new NatsClient("demo.nats.io");

var subscription = Task.Run(async () =>
{
    await foreach (var msg in nc.SubscribeAsync<string>(subject: "foo"))
    {
        Console.WriteLine($"Received: {msg.Data}");
        if (msg.Data == "exit")
        {
            break;
        }
    }
});

for (var i = 0; i < 10; i++)
{
    await Task.Delay(1000);
    Console.WriteLine("Publishing message...");
    await nc.PublishAsync(subject: "foo", data: $"{DateTime.Now} Hello, World!");
}

await nc.PublishAsync(subject: "foo", data: $"exit");

await subscription;

@mtmk
Copy link
Collaborator

mtmk commented Nov 26, 2024

@mtmk
Copy link
Collaborator

mtmk commented Nov 26, 2024

btw thanks for the heads up @matteoventuri7 we'll add a bit to readme about the server.

edit

@mtmk mtmk mentioned this issue Nov 26, 2024
@mtmk mtmk closed this as completed in #683 Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants