apache / openwhisk-apigateway

Apache OpenWhisk API Gateway service for exposing actions as REST interfaces.
https://openwhisk.apache.org/
Apache License 2.0
64 stars 45 forks source link

Support for invocations based on websocket messages #325

Open mhamann opened 5 years ago

mhamann commented 5 years ago

Provide a mechanism by which API invocations (e.g. to a Whisk action) may be triggered by a message to a websocket.

The basic approach could be something like:

There may also be a case where the API developer may wish the gateway to proxy the entire websocket connection through to the backend. This could be detected via the proxy policy having a ws:// or wss:// protocol in the target URL. Some work would still need to be done there to define how that works, but that would enable push messages from a backend. This is a more advanced use-case that probably needs addition design.

jthomas commented 5 years ago

This would be an excellent feature of the API Gateway 👏. Websocket support is something developers ask about a lot...

Another (more difficult) thing to consider is how to make it possible for messages to be sent to listening clients outside of responding to an incoming event. If we take one of the most common examples for a websocket application, a chat application, how could we enable this feature?

Ideally, an action could call the API Gateway service with a message to pass to one or more clients currently connected to pass messages that arrived from other sources.

mdeuser commented 5 years ago

@mhamann - can you describe the web socket use case(s) being addressed? i've been thinking that web sockets and serverless are kind of at odds with each other... but there may be a use case that bridges this gap.

mhamann commented 5 years ago

@jthomas all very good points.

Since the WebSocket RFC doesn't provide any mechanism by which to supply message headers, I think we might have to implement something custom.

One option would be to define a precise error format (e.g. always JSON with certain properties) that clients must be able to check for and process if something goes wrong.

Another possibility would be to allow the API creator to specify another action / URL that could process an error message and return a message in any format. Even so, something could go wrong with that invocation that would require falling back to the first option.

The chat scenario is interesting as well. A few things have occurred to me as to how we might handle that type of flow, but I'm going to think about it a bit further before writing anything down.

@mdeuser I think there are potentially a number of desirable use cases around this. If the notion of serverless computing is designed to replace many traditional application models, then WebSockets would be integral to the platform.

James already mentioned the chat app example, which would be really cool to duplicate in a serverless context.

Other examples:

jthomas commented 5 years ago

One option would be to define a precise error format

This seems like the most sensible option. Returning the message with an error identifer and message to indicate they need to re-send?

If we can support both in-bound and out-bound messages this would be a huge feature that the other platforms do not support.

mdeuser commented 5 years ago

@mhamann - taking the real-time stock ticker as an example.. are you thinking that the api gw would handle the connection and connection keep alives to the client and periodically invoke the backend action to obtain the latest stock quote and then sending that result to the client inside a websocket message?

rabbah commented 5 years ago

AWS just announced this.

mhamann commented 5 years ago

Is there a link to the announcement?

jthomas commented 5 years ago

I can't find any documentation about this feature in AWS at the moment. We'll have to wait and see what their implementation looks like. It'll be interesting to see how they handle errors and whether they support sending messages back outside of the invocation response.

mhamann commented 5 years ago

Here's a great overview of how this works. Very similar to what we proposed, but with a couple of missing pieces filled in: https://serverless.com/blog/api-gateway-websockets-support/

jthomas commented 5 years ago

More details now available: https://aws.amazon.com/blogs/compute/announcing-websocket-apis-in-amazon-api-gateway/

csantanapr commented 5 years ago

Here is an example of web chat app https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:729047367331:applications~simple-websockets-chat-app