@app.websocket('/temperature/ws')
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = fetch_data_db() # Fetch data from Cosmos Mongodb
await websocket.send_json(data)
await asyncio.sleep(1) # Add a delay to prevent overwhelming the client
So, that is a very simple websocket endpoint that I want to use for my API. But for some reason, when I tried the endpoint in postman. I got this:
wss://arduino-temp-api.replit.app/temperature/ws
Error: Unexpected server response: 404
Handshake Details
Request URL: https://arduino-temp-api.replit.app/temperature/ws
Request Method: GET
Status Code: 404 Not Found
Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: hCoalH2sqvSzKvcW+M2Bww==
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: arduino-temp-api.replit.app
Response Headers
Content-Length: 22
Content-Type: application/json
Date: Tue, 21 Nov 2023 12:45:14 GMT
Server: Google Frontend
Strict-Transport-Security: max-age=63072000; includeSubDomains
Traceparent: 00-9cc6e035935df382ad3f9cbc74c2286b-d2e977cd4596d871-01
X-Cloud-Trace-Context: 9cc6e035935df382ad3f9cbc74c2286b/15197810141057964145;o=1
Via: 1.1 google
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Connection: close
Obviously, my primary domain uses https. So I understand that trying to establish a websocket connection to a https domain will not work (Obviously).
But this led me down into a rabbit hole, I have checked the domain settings, and yes. I could see that one can add subdomains. And yes, I am aware that I can link a domain. But that would mean that
A: I would have to get a domain elsewhere and link it, doing the DNS settings stuff etc or:
B: Create a wss domain using replit, but that leads us to our next problem
So I decided to create a new replit project with only my socket endpoints. I checked the deployment settings. And I couldn’t see anything that pointed towards creating a wss domain. (And now before you ask, one can not just simply add “wss://” when creating a primary domain. Because when creating a domain this is what the error message from replit says:
“Can only contain alphanumeric characters and hyphens” )
As you can see, I feel pretty lost here. I tried googling it, but this seems to be a very unique problem.
When googling I either encountered TicTacToe tutorials that aren’t helpful at all, or nothing at all. The least useful stuff I found this this guy:Custom domain websocket
Who literally just said: “solved using express” or whatever.