question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting websocket port

See original GitHub issue

First check

  • I added a very descriptive title to this issue.
  • [*] I used the GitHub search to find a similar issue and didn’t find it.
  • [*] I searched the FastAPI documentation, with the integrated search.
  • [*] I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • [*] I already read and followed all the tutorial in the docs and didn’t find an answer.
  • [*] I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • [*] After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug

Port

How to add port in a websocket. I want specifically mention a port in the websocket server. I can specity the port from JS while connecting to a websocket, but can’t I do it from the server side. Is there any default value? Can I change that default value?

@app.websocket("/ws")
async def websocketEndpoint(websocket: WebSocket):
    print("Awaiting clients connection")
    await websocket.accept()
    while True:
        try:
            await websocket.receive_text()
            resp = {'value': random.uniform(0,1)}
            await websocket.send_json(resp)
        except Exception as e:
            print('error:', e)
            break
    print("connection ended")

Environment

  • OS: [e.g. Linux / Windows / macOS]: linux 18.04
  • FastAPI Version [e.g. 0.3.0]: 0.65.1

To know the FastAPI version use:

  • Python version: 3.8

To know the Python version use:

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
chandu1263commented, May 27, 2021

Thank you so much! I finally able to understand what I was doing wrong. That’s a great code and very nice explanation.

1reaction
roveocommented, May 21, 2021

I think websockets’ endpoints run on the same port as your http server, so in JS you specify the same port that you specify when calling uvicorn.run (or whatever else you’re using).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing WebSocket servers - Web APIs | MDN
A WebSocket server is nothing more than an application listening on any port of a TCP server that follows a specific protocol.
Read more >
Spring 4 WebSocket configure port - Stack Overflow
Using Spring 4 I need configure WebSocket use other port than HTTP. In other words by default user access to HTTP and WebSocket...
Read more >
Configure WebSocket connections
Configure a WebSocket connection · In the Policy Studio tree, select a list of relative paths (for example, Listeners > API Gateway >...
Read more >
WebSocket <webSocket> | Microsoft Learn
Windows 8 or Windows 8.1 · On the Start screen, move the pointer all the way to the lower left corner, right-click the...
Read more >
WebSocket Connection Configuration - Oro Documentation
WebSocket Connection Configuration · Configure a Web Server to ensure messages interaction between the WebSocket server and the clients. · Configure WebSocket- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found