title: How to implement a server "callback" on gRPC
body: <p>I make a POC on gRPC to see what its possible to do and migrate a WCF application on .Net 5</p>
  <p>POC is a simple chat, send new message and all clients get message</p>
  <p>Shared code</p>
  <pre><code>public interface IChatService
  {
      IAsyncEnumerable&lt;MessageResponse&gt; Subscribe();
      ValueTask PostMessage(MessageRequest msg);
  }
  
  [ProtoContract]
  public class MessageRequest
  {
      public MessageRequest(string user, string msg)
      {
          User = user;
          Msg = msg;
      }
  
      [ProtoMember(1)]
      public string Msg { get; }
  
      [ProtoMember(2)]
      public string User { get; }
  }
  
  [ProtoContract]
  public class MessageResponse
  {
      public MessageResponse(string user, string msg)
      {
          User = user;
          Msg = msg;
          Date = DateTime.Now;
      }
  
      [ProtoMember(1)]
      public string Msg { get; }
  
      [ProtoMember(2)]
      public string User { get; }
  
      [ProtoMember(2)]
      public DateTime Date { get; }
  }
  </code></pre>
  <p>Client-side code :</p>
  <pre><code>        using var http = GrpcChannel.ForAddress(&quot;http://localhost:10042&quot;);
  
          var chatService = http.CreateGrpcService&lt;IChatService&gt;();
  
          await foreach (var msg in chatService.Subscribe())
          {
              Console.WriteLine($&quot;{msg.Date} : {msg.User} - {msg.Msg}&quot;);
          }
  
          await chatService.PostMessage(new MessageRequest(&quot;User1&quot;, &quot;MyMessage&quot;));
  </code></pre>
  <p>Server-side Code :</p>
  <pre><code>public class ChatService : IChatService
  {
      private static readonly List&lt;MessageResponse&gt; OldMessages = new List&lt;MessageResponse&gt;();
  
      public async IAsyncEnumerable&lt;MessageResponse&gt; Subscribe()
      {
          foreach (var msg in OldMessages)
          {
              await Task.Delay(10);
              yield return msg;
          }
      }
  
      public async ValueTask PostMessage(MessageRequest msg)
      {
          await Task.Delay(10);
          OldMessages.Add(new MessageResponse(msg.User, msg.Msg));
      }
  }
  </code></pre>
  <p>In the idea, Subscribe method send to client when a new message is post
  and PostMessage method add a message to the chat (obvious)</p>
  <p>But i don't understand how on server-side code i can &quot;add&quot; message to the IAsyncEnumerable return by Subscribe ?
  With proto, it will be someyhing like that :</p>
  <pre><code>service ChatService {
    rpc Subscribe (request) returns (stream response);
  }
  </code></pre>
  <p>Thanks for your help !</p>