Wednesday, 22 June 2022

How to use AWS Websocket API Gateway Lambda endpoint callback function

When the client sends a websocket message to AWS websocket API Gateway the websocket Lambda handler function receives three arguments: event, context and callback:

import type { APIGatewayProxyEvent, Context, } from "aws-lambda";

export class Handler {
  constructor() {}

  async execute(event: APIGatewayProxyEvent, context: Context, callback: any): Promise<void> {
    console.log(`${event}, ${context}, ${callback}`);

    if (!event.requestContext || !event.requestContext.connectionId) {
      callback(null, { statusCode: 400, body: "Missing RequestContext" });
    };

    callback(null, {statusCode: 200, body: 'Your message was received'});
  }
}

const Handler = new Handler(service);
async function handler(event: APIGatewayProxyEvent, context: Context, callback: any): Promise<void> {
  return await Handler.execute(event, context, callback);
}
export default handler;

The Handler checks if the event that it received contains the value for event.requestContext.connectionId. If not, it calls the callback function passing null as the first argument and a message containing the 400 statusCode and bodywith message telling that the connectionId is missing:

callback(null, { statusCode: 400, body: "Missing RequestContext" });

Unfortunately, I was not able to locate any AWS documentation describing how exactly we should be using this callback function. Instead I've used some code examples found on Internet.

I was expecting that calling the callback function passing it 400 status will make the Handler function exit and raise an exception. But it doesn't happen. The handler just keeps going executing the next lines like nothing happened and callback function was never called.

  1. What is a real purpose for the callback function?
  2. How do we return an error message from websocket Handler function letting the client application know about the occurred problem?
  3. How do we stop the Handler from executing the rest of the code if there was an exception or invalid parameter received?
  4. Should the websocket Handler return any value or should it be void - no value returned?

Please post your answer as a script example or as a snippet so we could test it.

P.S. The doc posted at https://aws.amazon.com/blogs/compute/announcing-websocket-apis-in-amazon-api-gateway/ says that:

The backend can send messages to specific users via a callback URL that is provided after the user is connected to the WebSocket API.

And there is a snippet showing it:

exports.handler = function(event, context, callback) {
  var putParams = {
    TableName: process.env.TABLE_NAME,
    Item: {
      connectionId: { S: event.requestContext.connectionId }
    }
  };

  DDB.putItem(putParams, function(err, data) {
    callback(null, {
      statusCode: err ? 500 : 200,
      body: err ? "Failed to connect: " + JSON.stringify(err) : "Connected"
    });
  });
};


from How to use AWS Websocket API Gateway Lambda endpoint callback function

Tuesday, 21 June 2022

Is it possible to only convert a dart function to javascript

I am currently using the following package where the readme illustrates the following

final bool loaded = await JsIsolatedWorker().importScripts(['test.js']);

I am using the isolate worker package so my code can work on web and outside of web. I would like to generate javascript code from my dart code. I have one file with a top level function and I use

dart compile js -O0 -o test.js test.dart

which I found here

https://dart.dev/tools/dart2js

this is my dart file

void main(List<String> args) {
  doComputation('');
}
            
String doComputation(String input) {
  return 'output';
}

I can generate javascript only if I have a main function but this generates a javascript file where the doComutation is not a top level function, so I am not sure if the package can call the function. It looks like it generates an entire program instead of just generating one function.

The generated file is too long to post

So what my question comes down to is this. Is there a way to generate javascript from dart for 1 function with its dependencies included instead of having to generate the entire program? So that I can call this function from dart.



from Is it possible to only convert a dart function to javascript

AGP: How to tell gradle to pick a specific dependency variant

Assume I have an app with two build types debug and release. And there is an artifact in the maven repository that was published using the gradle variants API. When you look into the .module file you can find

    {
      "name": "anotherBuildTypeVariantCustomApiPublication",
      "attributes": {
        "com.android.build.api.attributes.BuildTypeAttr": "anotherBuildType",
        "org.gradle.category": "library",
        "org.gradle.dependency.bundling": "external",
        "org.gradle.libraryelements": "aar",
        "org.gradle.usage": "java-api"
      },
      "files": [
        {
          "name": "libraryname-0.1-anotherBuildType.aar",
          "url": "libraryname-0.1-anotherBuildType.aar",
          ....
        }
      ]
    }

How to tell gradle to pick anotherBuildType variant but not having to create a new variant of my app?

Currently gradle complains with

Variant 'anotherBuildTypeVariantCustomApiPublication' capability com.example:libraryname:0.1:
         - Incompatible because this component declares an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'anotherBuildType' and the consumer needed a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug'


from AGP: How to tell gradle to pick a specific dependency variant