Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Working with async functions #350

Open
grahamburgsma opened this issue Mar 26, 2019 · 3 comments
Open

Working with async functions #350

grahamburgsma opened this issue Mar 26, 2019 · 3 comments
Labels
enhancement New feature or request

Comments

@grahamburgsma
Copy link

Since the rest of Vapor is very asynchronous with many tasks returning futures, it makes it difficult to work with web sockets. Maybe this just requires more examples in the documentation, though it would be nice if web sockets got some love like the rest of Vapor.

If you need to do a query in one of the following examples, there is no where to return the future. I've run in to leaking promises and other issues because of this.

func onUpgrade(ws: WebSocket, req: Request) throws
ws.onText { (ws, text) in
    // no where to return future
}

Could this get addressed in Vapor 4?

@tanner0101 tanner0101 transferred this issue from vapor/websocket-kit Mar 26, 2019
@tanner0101 tanner0101 added the enhancement New feature or request label Mar 26, 2019
@tanner0101
Copy link
Member

This could be possible, if we can figure out a sensible way for the closure to handle a failed future. Something like logging the error then closing the WebSocket?

I'm not sure how useful that would be though since one would probably want better error handling logic for their client's sake. Which you can do currently with something like:

ws.onText { (ws, text) in
    someAsyncThing().do { res in
        ws.send("res is: \(res")
    }.catch { error in
        ws.send("oops: \(error)")
        ws.close()
    }
}

@calebkleveter
Copy link
Member

I think a WebSocket.onError handler would be nice:

ws.onError( (ws, error) in
    logger.error(error.localizedDescription)
    ws.close()
}

@grahamburgsma
Copy link
Author

I like @calebkleveter's idea, though don't know if that would be possible. Plus we already have WebSocket.onError and it's used for when the connection itself has an error.

But one place to handle errors from futures, or maybe an error middleware for web sockets would be nice.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants