class_docstr: A custom http input channel.
  
  This implementation is the basis for a custom implementation of a chat
  frontend. You can customize this to send messages to Rasa and
  retrieve responses from the assistant.
class_docstr_tok: ["A","custom","http","input","channel",".","This","implementation","is","the","basis","for","a","custom","implementation","of","a","chat","frontend",".","You","can","customize","this","to","send","messages","to","Rasa","and","retrieve","responses","from","the","assistant","."]
docstr: Streams response to the client.
  
   If the stream option is enabled, this method will be called to
   stream the response to the client
  
  Args:
      on_new_message: sanic event
      text: message text
      sender_id: message sender_id
      input_channel: input channel name
      metadata: optional metadata sent with the message
  
  Returns:
      Sanic stream
docstr_tok: ["Streams","response","to","the","client",".","If","the","stream","option","is","enabled",",","this","method","will","be","called","to","stream","the","response","to","the","client","Args",":","on_new_message",":","sanic","event","text",":","message","text","sender_id",":","message","sender_id","input_channel",":","input","channel","name","metadata",":","optional","metadata","sent","with","the","message","Returns",":","Sanic","stream"]
code: def stream_response(
      self,
      on_new_message: Callable[[UserMessage], Awaitable[None]],
      text: Text,
      sender_id: Text,
      input_channel: Text,
      metadata: Optional[Dict[Text, Any]],
  ) -> Callable[[Any], Awaitable[None]]:
      """Streams response to the client.
  
       If the stream option is enabled, this method will be called to
       stream the response to the client
  
      Args:
          on_new_message: sanic event
          text: message text
          sender_id: message sender_id
          input_channel: input channel name
          metadata: optional metadata sent with the message
  
      Returns:
          Sanic stream
      """
  
      async def stream(resp: Any) -> None:
          q: Queue = Queue()
          task = asyncio.ensure_future(
              self.on_message_wrapper(
                  on_new_message,
                  text,
                  q,
                  sender_id,
                  input_channel,
                  metadata,
              )
          )
          while True:
              result = await q.get()
              if result == "DONE":
                  break
              else:
                  await resp.write(json.dumps(result) + "\n")
          await task
  
      return stream
  
imports: import asyncio
  import copy
  import inspect
  import json
  import logging
  import structlog
  from asyncio import Queue, CancelledError
  from sanic import Blueprint, response
  from sanic.request import Request
  from sanic.response import HTTPResponse, ResponseStream
  from typing import Text, Dict, Any, Optional, Callable, Awaitable, NoReturn, Union
  import rasa.utils.endpoints
  from rasa.core.channels.channel import InputChannel, CollectingOutputChannel, UserMessage
label_desc: Use image node_id 5 for calling the RestInput obj's underlying member method code with example usage: obj.stream_response(on_new_message, text, sender_id, input_channel, metadata) and returns: stream