-
Notifications
You must be signed in to change notification settings - Fork 954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Socket fixes and improvements #485
Conversation
I see there's issues with how the transports defaults are pulled out. I'm not exactly sure what the best option is for lists of strings, but I can simply make the option a string and that will fix the errors. Let me know. Edit: actually that's what I'll do. The additional complexity isn't necessary seeing as this is a JS string list to begin with. |
Needs more work to get the CORS config be reasonable. Currently it can be either a string, an empty list, or a list of strings. |
Ok should be done now, let me know your thoughts. |
Are you getting this error: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Just some nits and I think this needs a rebase
pynecone/app.py
Outdated
@@ -35,6 +35,9 @@ class App(Base): | |||
# The Socket.IO AsyncServer. | |||
sio: AsyncServer = None | |||
|
|||
# The socket app. | |||
socket_app: ASGIApp = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type should be Optional[ASGIApp]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Done.
pynecone/app.py
Outdated
@@ -47,7 +50,7 @@ class App(Base): | |||
# Middleware to add to the app. | |||
middleware: List[Middleware] = [] | |||
|
|||
# events handlers to trigger when a page load | |||
# Events handlers to trigger when a page loads. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Event handlers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
I’ve gotten the opposite error before. I did a bunch of investigation and while it looks like an error caused by the back end, it’s actually an error triggered from the browser-end. It happened to me when I had an old window open which was trying to connect to the websocket interface using old front end code. Why that should cause such a backend error is a uvicorn mystery. I’m not 100% sure this is the cause in your case, but try closing old windows and tabs with Pynecone or refreshing them. I suspect the cause is the old Pynecone front end socket code trying to connect. I did that and haven’t seen this type of error again. |
Wierd I exited out of all browsers and still seems to be getting it, I think @picklelo is getting it to |
I'll look into it rn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, the errors are all gone now!
All Submissions:
Type of change
Please delete options that are not relevant.
New Feature Submission:
Changes To Core Features:
Description of changes:
Users can now select Socket.IO transport methods
A new option has been added to
pcconfig.py
pc.Config
backend_transports=pc.Transports.POLLING_WEBSOCKET
There are 4 options:
pc.Transports.POLLING_WEBSOCKET
– the default, initially connect to the back end using http/s requests (long polling) and upgrade to websocketspc.Transports.WEBSOCKET_POLLING
– connect using websockets outright, and fall back to long polling if they're blockedpc.Transports.WEBSOCKET_ONLY
– connect using websockets only, often fasterpc.Transports.POLLING_ONLY
– connect using long polling only (don't use websockets)Additional configuration options for backend connections
cors_allowed_origins=["*"]
– List of origins that are allowed to connect to the backend API. It will be useful to set this to a non-catchall on production servers.Example usage when on a production server:
cors_allowed_origins=["https://website.com", "https://myothersite.com"],
cors_credentials=True
– Whether credentials (cookies, authentication) are allowed in requests to the backend API.polling_max_http_buffer_size=1000*1000
– The maximum size of a message when using the polling backend transport in bytes. Defaults to 1MB. This is useful to increase if you want to send images and bigger data sets using long polling.New state getters which return session id, client IP and client headers
This is useful for various reasons such as wanting to control API access or rates by IP, getting the visitor's browser, language settings, etc.
Example usage:
Called using
Note that these default to reasonable blanks. These variables only return useful information upon firing of the first event, which usually happens on page load anyway. It's still important to check these have been obtained before using them.
Fixes
Closes #481.
The socket.io app was previously mounted on
/
which caused problems with dynamic routes. It has now been moved to/event
proper. No server re-configuration is necessary as the API endpoint remains the same. Initially I thought this wasn't doable but it's possible to set the socket.io path to""
.Tested locally and a production server.