WebSockets support only available within WebSockets methods (ValueError: WebsocketAPI.configure must be called before using the WebsocketAPI)
- Dominant language
- Python
- Stars
- 11.1k
- Forks
- 1k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 2
Description
It seems that WebSockets support only available within WebSockets methods. From other application code, one gets:
```
ValueError: WebsocketAPI.configure must be called before using the WebsocketAPI
```
This may seem alright, but application code may need to communicate with clients connected over WebSockets.
I don't think it's too much to ask to have this enabled by default.
To work around this, what I've done is to have a script put my Chalice exports into `chalicelib/chalice_exports.py` which looks like:
```py
exports = {
"dev": {
"rest_api": {
"name": "rest_api",
"resource_type": "rest_api",
"rest_api_id": "abc123rd",
"rest_api_url": "https://abc123rd.execute-api.ap-southeast-1.amazonaws.com/api-dev/"
},
"websocket_api": {
"name": "websocket_api",
"resource_type": "websocket_api",
"websocket_api_url": "wss://abc123wd.execute-api.ap-southeast-1.amazonaws.com/api-dev/",
"websocket_api_id": "abc123wd"
}
},
"prod": {
"rest_api": {
"name": "rest_api",
"resource_type": "rest_api",
"rest_api_id": "abc123pr",
"rest_api_url": "https://abc123pr.execute-api.ap-southeast-1.amazonaws.com/api/"
},
"websocket_api": {
"name": "websocket_api",
"resource_type": "websocket_api",
"websocket_api_url": "wss://abc123pw.execute-api.ap-southeast-1.amazonaws.com/api/",
"websocket_api_id": "abc123pw"
}
}
}
```
(I actually use Chalice with Amplify, so this had to be done to provide the same information to the front end.)
Then I have somewhere in my application code:
```py
STAGE = os.environ.get('STAGE', 'dev')
# ...
websocket_api = None
def inject_websocket_api(_websocket_api):
global websocket_api
websocket_api = _websocket_api
if websocket_api._endpoint is None:
log.info('-' * 40)
log.info(f'WebSockets Endpoint not set. Configuration needed.')
current_chalice_exports = chalice_exports.exports.get(STAGE)
if current_chalice_exports is not None:
websocket_api_domain = current_chalice_exports['websocket_api']['websocket_api_url'].split('/')[2]
websocket_api_stage = current_chalice_exports['websocket_api']['websocket_api_url'].split('/')[3]
websocket_api.configure(websocket_api_domain, websocket_api_stage)
log.info(f' - Configuring WebSockets: domain={websocket_api_domain}, stage={websocket_api_stage}')
else:
log.info(f' - Information to configure WebSockets not available')
log.info('-' * 40)
else:
log.info('-' * 40)
log.info(f'WebSockets Configured: websocket_api._endpoint={websocket_api._endpoint}')
log.info('-' * 40)
```
This is called from `app.py` as such:
```py
chalicelib.websockets.inject_websocket_api(app.websocket_api)
```
(Sorry, a little lazy to edit this.)
Contributor guide
Research direction
Start with the app.py call to chalicelib.websockets.inject_websocket_api and the WebsocketAPI.configure error described in the issue. Trace how the WebSocket API is initialized outside WebSocket methods, then determine the expected default configuration behavior and add coverage showing application code can communicate with connected clients without the manual workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, python
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100