Ivan Roskoshnyi
04/25/2022, 2:25 PMPub/Sub
using aws-amplify
npm package?
I did back-end part thankfully to this page https://serverless-stack.com/examples/how-to-use-pub-sub-in-your-serverless-app.html
but I can't connect it with front-end
. Official docs https://docs.amplify.aws/lib/pubsub/getting-started/q/platform/js/ doesn't cover a step how to configure pub/sub on the front-end using aws-amplify
npm package. It assumes to use aws cli
thdxr
04/25/2022, 2:27 PMthdxr
04/25/2022, 2:27 PMIvan Roskoshnyi
04/25/2022, 2:44 PMaws-amplify
?thdxr
04/25/2022, 2:46 PMIvan Roskoshnyi
04/25/2022, 2:46 PMthdxr
04/25/2022, 2:51 PMexport function Realtime(ctx: StackContext) {
const fn = new Function(ctx.stack, "fn", {
handler: "services/realtime/authorizer.handler",
permissions: ["iot"],
})
const authorizer = new iot.CfnAuthorizer(ctx.stack, "authorizer", {
status: "ACTIVE",
authorizerFunctionArn: fn.functionArn,
signingDisabled: true,
})
fn.addPermission("IOTPermission", {
principal: new ServicePrincipal("<http://iot.amazonaws.com|iot.amazonaws.com>"),
sourceArn: authorizer.attrArn,
action: "lambda:InvokeFunction",
})
return {
endpoint:
ENDPOINTS[ctx.app.stage as keyof typeof ENDPOINTS] || ENDPOINTS.dev,
}
}
Note I'm using a custom authorizer - this basically can return any IAM policy to grant to whoever is connecting. Unfortunately the IoT endpoints are retreivable in code so I had to hardcode a list of them per aws account - which is what that return type is
Then on the frontend:
import { Client, Message } from "paho-mqtt"
import { useEffect } from "react"
import { v4 } from "uuid"
const Realtime = new Client(
`wss://${
import.meta.env.VITE_REALTIME_ENDPOINT
}/mqtt?x-amz-customauthorizer-name=authorizer`,
v4()
)
console.log(import.meta.env)
const onMessage: ((msg: Message) => void)[] = []
Realtime.onMessageArrived = (evt) => {
onMessage.forEach((cb) => cb(evt))
console.log(evt)
}
Realtime.onMessageDelivered = console.log
Realtime.onConnectionLost = console.log
Realtime.connect({
useSSL: true,
mqttVersion: 4,
reconnect: true,
onSuccess: () => {
Realtime.subscribe("#")
},
onFailure: console.log,
})
This example is subscribing to 100% of topics. From here the backend can publish events and the frontend will get itthdxr
04/25/2022, 2:52 PMthdxr
04/25/2022, 2:52 PMexport const handler = async (evt: any) => {
return {
isAuthenticated: true,
principalId: "asd",
disconnectAfterInSeconds: 86400,
refreshAfterInSeconds: 300,
policyDocuments: [
{
Version: "2012-10-17",
Statement: [
{
Action: "iot:*",
Effect: "Allow",
Resource: "*",
},
],
},
],
}
}
Kujtim Hoxha
04/25/2022, 3:59 PMthdxr
04/25/2022, 4:04 PMKujtim Hoxha
04/25/2022, 4:05 PMKujtim Hoxha
04/25/2022, 4:06 PMI never look at AWS pricing lol, I deal with it laterConsidering ^ I am assuming nothing came up yet for IOT so it is good to go 😂
Ivan Roskoshnyi
04/25/2022, 5:41 PMIvan Roskoshnyi
04/25/2022, 5:41 PMweb socket
instead of PubSub?thdxr
04/25/2022, 5:43 PMthdxr
04/25/2022, 5:43 PMimport { Function, StackContext } from "@serverless-stack/resources"
import { CfnAuthorizer } from "aws-cdk-lib/aws-iot"
import { ServicePrincipal } from "aws-cdk-lib/aws-iam"