Skip to content

Switch to sansio websockets back-end#289

Merged
rwb27 merged 2 commits intomainfrom
websockets-sansio
Mar 26, 2026
Merged

Switch to sansio websockets back-end#289
rwb27 merged 2 commits intomainfrom
websockets-sansio

Conversation

@rwb27
Copy link
Collaborator

@rwb27 rwb27 commented Mar 5, 2026

Uvicorn uses the now deprecated websockets.legacy back end by default. It also supports the newer sansio interface. As far as I can tell, either works, so I'm not sure why legacy is the default: backwards compatibility I guess.

I'd like to switch to the newer implementation now, before we are using websockets in anger, as it feels wrong to start out with deprecation warnings in the test suite! I don't believe we will notice the difference, but hopefully the test suite will tell. Websockets aren't used yet by the OFM.

@barecheck
Copy link

barecheck bot commented Mar 5, 2026

Barecheck - Code coverage report

Total: 96.34%

Your code coverage diff: 0.00% ▴

Uncovered files and lines
FileLines
src/labthings_fastapi/server/cli.py102, 184

@rwb27 rwb27 added this to the v0.2.0 milestone Mar 9, 2026
Uvicorn uses the now deprecated `websockets.legacy` back end by default. It also supports the newer `sansio` interface. As far as I can tell, either works, so I'm not sure why legacy is the default: backwards compatibility I guess.

I'd like to switch to the newer implementation now, before we are using websockets in anger, as it feels wrong to start out with deprecation warnings in the test suite!
@rwb27 rwb27 force-pushed the websockets-sansio branch from a1db57e to f1dc80d Compare March 23, 2026 22:42
@rwb27 rwb27 requested a review from julianstirling March 25, 2026 10:17
Copy link
Contributor

@julianstirling julianstirling left a comment

Choose a reason for hiding this comment

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

This generally make sense. I think the worry is there will be inconsistent behaviour if people follow the example in tutorial/writing_a_thing.rst. Even though websockets are not in use it is probably worth updating that example.

This also relates to #263 as the OFM has its own uvicorn.run lines. I am not sure how best to wrap this so it can be enabled. But at the minimum I'd update the docs.

This makes sure everything is consistent.
Copy link
Collaborator Author

@rwb27 rwb27 left a comment

Choose a reason for hiding this comment

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

This generally make sense. I think the worry is there will be inconsistent behaviour if people follow the example in tutorial/writing_a_thing.rst. Even though websockets are not in use it is probably worth updating that example.

This also relates to #263 as the OFM has its own uvicorn.run lines. I am not sure how best to wrap this so it can be enabled. But at the minimum I'd update the docs.

Thanks - I've updated the two places in the example code where I found uvicorn.run. I don't currently believe it's a problem if a different back-end is used, but obviously if we're testing against sansio it makes sense to use it elsewhere, and it's reasonable to be consistent.

I am starting to think that some sort of ThingServer.run() wrapper might be convenient: it's probably easier for people to find, and it means we can bundle in arguments that ought not to change. That would mean we change from:

uvicorn.run(server.app, port=5000, ws="websockets-sansio")

to

server.run(port=5000)

Maybe serve_with_uvicorn would be a clearer name for the function?

I could also see us wanting to add something like ThingServer.test_client or test_env for similar reasons.

I'm not proposing to add either in this PR, but it would be worth adding in a future one if you agree it's a reasonable way to go.

Copy link
Contributor

@julianstirling julianstirling left a comment

Choose a reason for hiding this comment

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

Thanks. I have made #298 for the final comment.

@rwb27 rwb27 merged commit d01f53e into main Mar 26, 2026
17 checks passed
@rwb27 rwb27 deleted the websockets-sansio branch March 26, 2026 13:47
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.

2 participants