I am trying to deploy a streamlit app and a websocket server on Heroku and I have been able to successfully do the same. However, the error occurs once I have made the deployment. When I open the Heroku app and check the logs from both the dynos (I have 2 dynos: web and worker). The worker dyno runs the websocket server. According to the logs the web application is unable to send any requests to the websocket server. I have also confirmed that the address I am connecting to in the app is the correct one. Please let me know where I am going wrong given this is the first deployment I am making.
Here is the code for my websocket_server.py
import os
import asyncio
import websockets
import signal
CONNECTED_CLIENTS = set()
async def handler(websocket, path):
CONNECTED_CLIENTS.add(websocket)
print(f"Client connected: {websocket.remote_address}")
try:
async for message in websocket:
print(f"Received message: {message}")
for client in CONNECTED_CLIENTS:
if client != websocket and client.open:
await client.send(message)
finally:
CONNECTED_CLIENTS.remove(websocket)
print(f"Client disconnected: {websocket.remote_address}")
async def main():
loop = asyncio.get_running_loop()
stop = loop.create_future()
loop.add_signal_handler(signal.SIGTERM, stop.set_result, None)
port = int(os.environ.get("PORT", "8001"))
async with websockets.serve(handler, "", port):
print(f"WebSocket server started on :{port}")
await stop
if __name__ == "__main__":
asyncio.run(main())
Here is the code for my streamlit app:
import streamlit as st
import asyncio
import websockets
async def send_command(command):
try:
async with websockets.connect("wss://hololens-sense-9bd80b459134.herokuapp.com/") as websocket:
await websocket.send(command)
print(f"Sent command : {command}")
except Exception as e:
print(f"Error sending command: {e}")
def send_command_async(command):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(send_command(command))
\ More code for the web app
Here is the setup.sh file and Procfile:
mkdir -p ~/.streamlit/
echo "
[general]n
email = "[email protected]"n
" > ~/.streamlit/credentials.toml
echo "
[server]n
headless = truen
port = $PORTn
enableCORS = falsen
n
" > ~/.streamlit/config.toml
web: sh setup.sh && streamlit run app.py --server.port=$PORT
worker: python websocket_server.py
I have tried explicitly specifying the port on the web application but that does not help. I have tried disabling CORS as well (I only followed someone else’s solution, I do not understand how this might have helped). I have implemented extensive logging and the only thing I get is this:
2024-08-19T18:16:51.256021+00:00 app[web.1]: Error sending slider_value:0.5: server rejected WebSocket connection: HTTP 200
2024-08-19T18:16:51.256470+00:00 app[web.1]: Traceback (most recent call last):
2024-08-19T18:16:51.256633+00:00 app[web.1]: File "/app/app.py", line 8, in send_command
2024-08-19T18:16:51.256638+00:00 app[web.1]: async with websockets.connect("wss://hololens-sense-9bd80b459134.herokuapp.com") as websocket:
2024-08-19T18:16:51.256639+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/websockets/legacy/client.py", line 629, in __aenter__
2024-08-19T18:16:51.256641+00:00 app[web.1]: return await self
2024-08-19T18:16:51.256641+00:00 app[web.1]: ^^^^^^^^^^
2024-08-19T18:16:51.256642+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/websockets/legacy/client.py", line 647, in __await_impl_timeout__
2024-08-19T18:16:51.256642+00:00 app[web.1]: return await self.__await_impl__()
2024-08-19T18:16:51.256642+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-08-19T18:16:51.256643+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/websockets/legacy/client.py", line 654, in __await_impl__
2024-08-19T18:16:51.256643+00:00 app[web.1]: await protocol.handshake(
2024-08-19T18:16:51.256652+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/websockets/legacy/client.py", line 325, in handshake
2024-08-19T18:16:51.256653+00:00 app[web.1]: raise InvalidStatusCode(status_code, response_headers)
2024-08-19T18:16:51.256666+00:00 app[web.1]: websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 200