Skip to content
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

Pass options for aiohttp client (websockets) #620

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion nats/aio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ async def connect(
inbox_prefix: Union[str, bytes] = DEFAULT_INBOX_PREFIX,
pending_size: int = DEFAULT_PENDING_SIZE,
flush_timeout: Optional[float] = None,
ws_client_options: Optional[dict] = None,
) -> None:
"""
Establishes a connection to NATS.
Expand Down Expand Up @@ -450,6 +451,9 @@ async def subscribe_handler(msg):
self._nkeys_seed = nkeys_seed
self._nkeys_seed_str = nkeys_seed_str

# Options to customize aiohttp client in case of websocket transport
self._ws_client_options = ws_client_options

# Customizable options
self.options["verbose"] = verbose
self.options["pedantic"] = pedantic
Expand Down Expand Up @@ -1348,7 +1352,9 @@ async def _select_next_server(self) -> None:
s.last_attempt = time.monotonic()
if not self._transport:
if s.uri.scheme in ("ws", "wss"):
self._transport = WebSocketTransport()
self._transport = WebSocketTransport(
self._ws_client_options
)
else:
# use TcpTransport as a fallback
self._transport = TcpTransport()
Expand Down
6 changes: 4 additions & 2 deletions nats/aio/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,15 @@ def __bool__(self):

class WebSocketTransport(Transport):

def __init__(self):
def __init__(self, client_options: Optional[dict] = None):
if not aiohttp:
raise ImportError(
"Could not import aiohttp transport, please install it with `pip install aiohttp`"
)
self._ws: Optional[aiohttp.ClientWebSocketResponse] = None
self._client: aiohttp.ClientSession = aiohttp.ClientSession()
self._client: aiohttp.ClientSession = aiohttp.ClientSession(
**client_options
)
self._pending = asyncio.Queue()
self._close_task = asyncio.Future()
self._using_tls: Optional[bool] = None
Expand Down