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

Creating Websocket Handler #24

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

jhileman07
Copy link
Collaborator

Description

We need a handler to run a websocket to be able to receive messages.

Testing

Attempted to create unit tests for certain functions and an integration test.

Impact

A websocket is necessary to listen to messages.

Other

Must ensure that the way async was handled won't affect the codebase.

@jhileman07 jhileman07 linked an issue Oct 16, 2024 that may be closed by this pull request
@jhileman07 jhileman07 force-pushed the jhileman/7-python-util-websocket-handler branch from c2b6e75 to 9d9d908 Compare October 16, 2024 09:07
websocket_client.connect()

# Wait for the connection to be established
asyncio.get_event_loop().run_until_complete(asyncio.sleep(1))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the client should be dealing with this internally. Maybe some sort of flag for when connect is called, and then dependent functions will spin if the flag is set but the connection hasn't been established

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a self._connected boolean flag to handle internally

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should expose connected via a property so you can spin like so

while not ws_client.connected:
    pass

poetry.lock Outdated Show resolved Hide resolved
@echavemann echavemann force-pushed the jhileman/7-python-util-websocket-handler branch from 030a081 to db3ca15 Compare October 17, 2024 22:05
Copy link
Member

@egelja egelja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks really great, small changes requested

from typing import Optional
import threading

logging.basicConfig(level=logging.INFO)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging should be configured in an application entrypoint, not in library code --- this would break previous configurations

Comment on lines +58 to +61
if isinstance(message, bytes):
message = message.decode("utf-8")
logger.info(f"Received message: {message}")
self.received_messages.append(message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be empty... exposing received_messages like this will lead to concurrency errors

Let clients impl their own message handling

logger.error(f"WebSocket connection error: {error}")
# reconnect logic
if self._loop:
self._loop.call_later(self.reconnect_interval, self.connect)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will spawn a new thread and loop without cleaning up the existing ones

Comment on lines +14 to +16
self.url: str = url
self.websocket: Optional[websockets.WebSocketClientProtocol] = None
self.reconnect_interval: int = reconnect_interval
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should all be private

if you want to expose url or reconnect interval, use a property

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python Util: Websocket Handler
4 participants