You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-web-pubsub/tutorial-build-chat.md
+12-18Lines changed: 12 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -114,16 +114,16 @@ You can test the server by running `dotnet run --urls http://localhost:8080` and
114
114
115
115
You may remember in the [publish and subscribe message tutorial](./tutorial-pub-sub-messages.md) the subscriber uses an API in Web PubSub SDK to generate an access token from connection string and use it to connect to the service. This is usually not safe in a real world application as connection string has high privilege to do any operation to the service so you don't want to share it with any client. Let's change this access token generation process to a REST API at server side, so client can call this API to request an access token every time it needs to connect, without need to hold the connection string.
116
116
117
-
1. Install dependencies and use [Secret Manager](/aspnet/core/security/app-secrets#secret-manager) tool for .NET Core to set the connection string. Run the below command, replacing `<connection_string>` with the one fetched in [previous step](#get-the-connectionstring-for-future-use).
117
+
1. Install dependencies.
118
118
119
119
```bash
120
120
dotnet add package Microsoft.Extensions.Azure
121
121
```
122
122
123
-
2. Add a `SampleChatHub.cs` class to handle hub events.
123
+
2. Add a `SampleChatHub` class to handle hub events. And DI the service middleware and service client inside `ConfigureServices`. Don't forget to replace `<connection_string>` with the one of your services.
@@ -132,23 +132,17 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
132
132
_serviceClient = serviceClient;
133
133
}
134
134
}
135
-
```
136
-
137
-
Here we use the `WebPubSubServiceClient<SampleChatHub>` to invoke rest API calls to service. This is bind with the DI method `AddWebPubSubServiceClient<SampleChatHub>()` in the next step.
138
-
139
-
3. DI the service middleware and service client inside `ConfigureServices` and don't forget to replace `<connection_string>` with the one of your services.
140
-
141
-
```csharp
135
+
142
136
public void ConfigureServices(IServiceCollection services)
143
137
{
144
138
services.AddWebPubSub(o => o.ServiceEndpoint = new ServiceEndpoint("<connection_string>"))
145
139
.AddWebPubSubServiceClient<SampleChatHub>();
146
140
}
147
141
```
148
142
149
-
`AddWebPubSubServiceClient<THub>` is used to inject the service client, with which you can generate client connection token and invoke service to do something when hub events are triggered.
143
+
`AddWebPubSubServiceClient<THub>` is used to inject the service client, with which we can generate client connection token and invoke service REST APIs when hub events are triggered.
150
144
151
-
4. Add a `/negotiate` API to the server inside `app.UseEndpoints` to generate the token.
145
+
3. Add a `/negotiate` API to the server inside `app.UseEndpoints` to generate the token.
152
146
153
147
```csharp
154
148
app.UseEndpoints(endpoints =>
@@ -172,7 +166,7 @@ You may remember in the [publish and subscribe message tutorial](./tutorial-pub-
172
166
173
167
You can test this API by running `dotnet run --urls http://localhost:8080` and accessing `http://localhost:8080/negotiate?id=<user-id>` and it will give you the full url of the Azure Web PubSub with an access token.
174
168
175
-
5. Then update `index.html` to include the following script to get the token from server and connect to service.
169
+
4. Then update `index.html` to include the following script to get the token from server and connect to service.
176
170
177
171
```html
178
172
<html>
@@ -497,9 +491,9 @@ Here we're using Web PubSub middleware SDK, there is already an implementation t
497
491
});
498
492
```
499
493
500
-
2. Go the `SampleChatHub.cs` file we created in previous step and add logic we'd like server to invoke service when subscribed events are triggered.
494
+
2. Go the `SampleChatHub` we created in previous step and override `OnConnectedAsync()` method we'd like server to invoke service when `connected` event is triggered.
@@ -644,7 +638,7 @@ Implement the `OnMessageReceivedAsync()` method in `SampleChatHub.cs`.
644
638
}
645
639
```
646
640
647
-
This event handler uses `WebPubSubServiceClient.SendToAllAsync()` to broadcast the received message to all clients. You can see in the end we returned `UserEventResponse`, which contains a message directly to the caller and make the WebHook request success. If you have extra logic to validate and would like to break this call, you can directly throw an exception here. The middleware will deliver the exception message to service and service will drop current connection.
641
+
This event handler uses `WebPubSubServiceClient.SendToAllAsync()` to broadcast the received message to all clients. You can see in the end we returned `UserEventResponse`, which contains a message directly to the caller and make the WebHook request success. If you have extra logic to validate and would like to break this call, you can throw an exception here. The middleware will deliver the exception message to service and service will drop current client connection.
648
642
649
643
2. Update `index.html` to add the logic to send message from user to server and display received messages in the page.
0 commit comments