- 
                Notifications
    You must be signed in to change notification settings 
- Fork 93
Add pubsub client sdk sample #466
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
          
     Open
      
      
            zackliu
  wants to merge
  2
  commits into
  Azure:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
zackliu:wps-client-sample1
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Client pub-sub with `Azure.Messaging.WebPubSub.Client` | ||
|  | ||
| ## Prerequisites | ||
|  | ||
| 1. [NET 6 or above](https://docs.microsoft.com/dotnet) | ||
| 2. Create an Azure Web PubSub resource | ||
|  | ||
| ## Overview | ||
|  | ||
| With client sdk `Azure.Messaging.WebPubSub.Client`, it's easy to communicate among clients and leverage reliable protocol by default to overcome transient connection drop. | ||
|  | ||
| ## Setup | ||
|  | ||
| Copy **Connection String** from **Keys** tab of the created Azure Web PubSub service, and replace the `<connection-string>` below with the value of your **Connection String**. | ||
|  | ||
|  | ||
|  | ||
| 1. Start Client subscriber | ||
|  | ||
| ```bash | ||
| cd clientsub | ||
| dotnet run -- "<ConnectionString>" pubsubhub | ||
| ``` | ||
|  | ||
| 2. Start Client publisher | ||
|  | ||
| ```bash | ||
| cd clientpub | ||
| dotnet run -- "ConnectionString>" pubsubhub | ||
| ``` | ||
|  | ||
| Start typing messages and you can see these messages are transferred to the client subscriber in real-time. | 
        
          
          
            46 changes: 46 additions & 0 deletions
          
          46 
        
  samples/csharp/clientpubsub-client-sdk/clientpub/Program.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
|  | ||
| using Azure.Messaging.WebPubSub; | ||
| using Azure.Messaging.WebPubSub.Clients; | ||
|  | ||
| namespace clientpub | ||
| { | ||
| class Program | ||
| { | ||
| static async Task Main(string[] args) | ||
| { | ||
| if (args.Length != 2) | ||
| { | ||
| Console.WriteLine("Usage: clientpub <connectionString> <hub>"); | ||
| return; | ||
| } | ||
| var connectionString = args[0]; | ||
| var hub = args[1]; | ||
|  | ||
| // Either generate the URL or fetch it from server or fetch a temp one from the portal | ||
| var serviceClient = new WebPubSubServiceClient(connectionString, hub); | ||
|  | ||
| var client = new WebPubSubClient(new WebPubSubClientCredential(async token => | ||
| { | ||
| return await serviceClient.GetClientAccessUriAsync(userId: "user1", roles: new string[] { "webpubsub.joinLeaveGroup.demogroup", "webpubsub.sendToGroup.demogroup" }); | ||
| })); | ||
|  | ||
| client.Connected += e => | ||
| { | ||
| Console.WriteLine($"Connected: {e.ConnectionId}"); | ||
| return Task.CompletedTask; | ||
| }; | ||
|  | ||
| await client.StartAsync(); | ||
|  | ||
| var streaming = Console.ReadLine(); | ||
| while (streaming != null) | ||
| { | ||
| await client.SendToGroupAsync("demogroup", BinaryData.FromString(streaming), WebPubSubDataType.Text); | ||
| streaming = Console.ReadLine(); | ||
| } | ||
| } | ||
| } | ||
| } | 
        
          
          
            13 changes: 13 additions & 0 deletions
          
          13 
        
  samples/csharp/clientpubsub-client-sdk/clientpub/clientpub.csproj
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|  | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| </PropertyGroup> | ||
|  | ||
| <ItemGroup> | ||
| <PackageReference Include="Azure.Messaging.WebPubSub" Version="1.3.0" /> | ||
| <PackageReference Include="Azure.Messaging.WebPubSub.Client" Version="1.0.0-beta.1" /> | ||
| </ItemGroup> | ||
|  | ||
| </Project> | ||
        
          
          
            46 changes: 46 additions & 0 deletions
          
          46 
        
  samples/csharp/clientpubsub-client-sdk/clientsub/Program.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|  | ||
| using Azure.Messaging.WebPubSub; | ||
| using Azure.Messaging.WebPubSub.Clients; | ||
|  | ||
| namespace clientsub | ||
| { | ||
| class Program | ||
| { | ||
| static async Task Main(string[] args) | ||
| { | ||
| if (args.Length != 2) | ||
| { | ||
| Console.WriteLine("Usage: clientsub <connectionString> <hub>"); | ||
| return; | ||
| } | ||
| var connectionString = args[0]; | ||
| var hub = args[1]; | ||
|  | ||
| // Either generate the URL or fetch it from server or fetch a temp one from the portal | ||
| var serviceClient = new WebPubSubServiceClient(connectionString, hub); | ||
|  | ||
| var client = new WebPubSubClient(new WebPubSubClientCredential(async token => | ||
| { | ||
| return await serviceClient.GetClientAccessUriAsync(userId: "user1", roles: new string[] { "webpubsub.joinLeaveGroup.demogroup", "webpubsub.sendToGroup.demogroup" }); | ||
| })); | ||
|  | ||
| client.Connected += e => | ||
| { | ||
| Console.WriteLine($"Connected: {e.ConnectionId}"); | ||
| return Task.CompletedTask; | ||
| }; | ||
|  | ||
| client.GroupMessageReceived += e => | ||
| { | ||
| Console.WriteLine($"Message received: {e.Message.Data}"); | ||
| return Task.CompletedTask; | ||
| }; | ||
|  | ||
| await client.StartAsync(); | ||
| await client.JoinGroupAsync("demogroup"); | ||
| Console.Read(); | ||
| } | ||
| } | ||
| } | 
        
          
          
            13 changes: 13 additions & 0 deletions
          
          13 
        
  samples/csharp/clientpubsub-client-sdk/clientsub/clientsub.csproj
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|  | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| </PropertyGroup> | ||
|  | ||
| <ItemGroup> | ||
| <PackageReference Include="Azure.Messaging.WebPubSub" Version="1.3.0" /> | ||
| <PackageReference Include="Azure.Messaging.WebPubSub.Client" Version="1.0.0-beta.1" /> | ||
| </ItemGroup> | ||
|  | ||
| </Project> | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reminder: update version when merge.