Skip to content

Commit bb1099a

Browse files
committed
Add nats.net pub-sub example
Signed-off-by: Byron Ruth <byron@nats.io>
1 parent c81e300 commit bb1099a

File tree

5 files changed

+122
-10
lines changed

5 files changed

+122
-10
lines changed

cmd/nbe/docs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var (
2727
Deno,
2828
Node,
2929
Rust,
30-
CSharp,
30+
DotNet,
3131
Java,
3232
Ruby,
3333
Elixir,

cmd/nbe/parse.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
Go = "go"
2222
Rust = "rust"
2323
Java = "java"
24-
CSharp = "csharp"
24+
DotNet = "dotnet"
2525
Deno = "deno"
2626
Node = "node"
2727
Bun = "bun"
@@ -40,7 +40,7 @@ var (
4040
Go: "Go",
4141
Rust: "Rust",
4242
Java: "Java",
43-
CSharp: "C#",
43+
DotNet: "C#",
4444
Deno: "Deno",
4545
Node: "Node",
4646
Bun: "Bun",
@@ -63,14 +63,15 @@ var (
6363
Node: "main.js",
6464
WebSocket: "main.js",
6565
Java: "Main.java",
66+
DotNet: "Main.cs",
6667
}
6768

6869
languageMultiCommentDelims = map[string][2]string{
6970
Go: {"/*", "*/"},
7071
// TODO: java has a few conventions..
7172
// https://www.oracle.com/java/technologies/javase/codeconventions-comments.html
7273
Java: {"/*", "*/"},
73-
CSharp: {"/**", "**/"},
74+
DotNet: {"/**", "**/"},
7475
Deno: {"/*", "*/"},
7576
Node: {"/*", "*/"},
7677
Bun: {"/*", "*/"},
@@ -84,7 +85,7 @@ var (
8485
Go: "//",
8586
Rust: "//",
8687
Java: "//",
87-
CSharp: "///",
88+
DotNet: "//",
8889
Deno: "//",
8990
Node: "//",
9091
Bun: "//",
@@ -162,10 +163,12 @@ var (
162163

163164
// One limitiation is that it does not currently handle trailing multi-line
164165
// comments, such as:
165-
// func() int {/*
166-
// a := 1
167-
// */
168-
// b := 2
166+
//
167+
// func() int {/*
168+
// a := 1
169+
// */
170+
// b := 2
171+
//
169172
// Since this code is scoped to well written examples, it should not be an issue
170173
// in practice.
171174
func parseLineType(lang, line string) LineType {
@@ -183,7 +186,7 @@ func parseLineType(lang, line string) LineType {
183186
}
184187
return NormalLine
185188

186-
case Go, CSharp, Java, Rust, C, Deno, Node, Bun:
189+
case Go, DotNet, Java, Rust, C, Deno, Node, Bun:
187190
if cStyleSingleCommentLineRe.MatchString(line) {
188191
return SingleCommentLine
189192
}

docker/dotnet/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# https://hub.docker.com/_/microsoft-dotnet
2+
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
3+
WORKDIR /source
4+
5+
# copy csproj and restore as distinct layers
6+
COPY *.csproj .
7+
RUN dotnet restore --use-current-runtime
8+
9+
# copy and publish app and libraries
10+
COPY . .
11+
RUN dotnet publish -c Release -o /app --use-current-runtime --self-contained false --no-restore
12+
13+
# final stage/image
14+
FROM mcr.microsoft.com/dotnet/runtime:7.0
15+
WORKDIR /app
16+
COPY --from=build /app .
17+
ENTRYPOINT ["dotnet", "example.dll"]

docker/dotnet/example.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<Title>NATS by Example</Title>
7+
<Nullable>enable</Nullable>
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="NATS.Client" Version="1.0.2"></PackageReference>
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
using NATS.Client;
8+
9+
// Create a new connection factory to create
10+
// a connection.
11+
Options opts = ConnectionFactory.GetDefaultOptions();
12+
opts.Url = "nats://nats:4222";
13+
14+
// Creates a live connection to the default
15+
// NATS Server running locally
16+
ConnectionFactory cf = new ConnectionFactory();
17+
IConnection c = cf.CreateConnection(opts);
18+
19+
// Setup an event handler to process incoming messages.
20+
// An anonymous delegate function is used for brevity.
21+
EventHandler<MsgHandlerEventArgs> h = (sender, args) =>
22+
{
23+
// print the message
24+
Console.WriteLine(args.Message);
25+
26+
// Here are some of the accessible properties from
27+
// the message:
28+
// args.Message.Data;
29+
// args.Message.Reply;
30+
// args.Message.Subject;
31+
// args.Message.ArrivalSubcription.Subject;
32+
// args.Message.ArrivalSubcription.QueuedMessageCount;
33+
// args.Message.ArrivalSubcription.Queue;
34+
35+
// Unsubscribing from within the delegate function is supported.
36+
args.Message.ArrivalSubcription.Unsubscribe();
37+
};
38+
39+
// The simple way to create an asynchronous subscriber
40+
// is to simply pass the event in. Messages will start
41+
// arriving immediately.
42+
IAsyncSubscription s = c.SubscribeAsync("foo", h);
43+
44+
// Alternatively, create an asynchronous subscriber on subject foo,
45+
// assign a message handler, then start the subscriber. When
46+
// multicasting delegates, this allows all message handlers
47+
// to be setup before messages start arriving.
48+
IAsyncSubscription sAsync = c.SubscribeAsync("foo");
49+
sAsync.MessageHandler += h;
50+
sAsync.Start();
51+
52+
// Simple synchronous subscriber
53+
ISyncSubscription sSync = c.SubscribeSync("foo");
54+
55+
// Using a synchronous subscriber, gets the first message available,
56+
// waiting up to 1000 milliseconds (1 second)
57+
Msg m = sSync.NextMessage(1000);
58+
59+
c.Publish("foo", Encoding.UTF8.GetBytes("hello world"));
60+
61+
// Unsubscribing
62+
sAsync.Unsubscribe();
63+
64+
// Publish requests to the given reply subject:
65+
c.Publish("foo", "bar", Encoding.UTF8.GetBytes("help!"));
66+
67+
// Sends a request (internally creates an inbox) and Auto-Unsubscribe the
68+
// internal subscriber, which means that the subscriber is unsubscribed
69+
// when receiving the first response from potentially many repliers.
70+
// This call will wait for the reply for up to 1000 milliseconds (1 second).
71+
m = c.Request("foo", Encoding.UTF8.GetBytes("help"), 1000);
72+
73+
// Draining and closing a connection
74+
c.Drain();
75+
76+
// Closing a connection
77+
c.Close();

0 commit comments

Comments
 (0)