Receiving CloudEvents with NextJS

Published December 15, 2021 #nextjs, #knative, #cloudevents

Need to receive cloudevents from your NextJS app? Me too! You need next-runtime.

Install

  npm i next-runtime cloudevents

In pages/indes.ts

And then:

  import { handle, json } from 'next-runtime';
  import { HTTP } from 'cloudevents';

  export const getServerSideProps = handle({
      async post({ req }) {
          console.log( req.body )
          console.log( req.headers )
        
          const receivedEvent = HTTP.toEvent({headers: req.headers, body: req.body })
          console.log( receivedEvent )
          return json({ message: 'Thanks for your submission!' });
      },
      async get() {
          return json( {message:"ok"} )
      }
  }))

Took me a while to sort out, but it wasn't so hard. Thanks meijer_s!

Read next

See also

NextJS with Prisma on Kubernetes

deploy as a knative service

Now that we have our cluster up and running, lets look at how to build and deploy a NextJS app on it, including the database. Create a NextJS app We'll scaffold out a TypeScript app. npx create-next-app@latest --typescript myapp cd myapp npm run dev Fireup a local data docker run -e POSTGRES_PASSWORD=awesome_password -p 5432:5432 postgres Install prisma We'll add the npm packages to our project.

Read more

Setting up knative eventing

lets send some messages

Here's a walk through of how to setup knative eventing. I couldn't find this information in one place and it took longer than I expected to sort it out. There are a couple of ways you can use eventing. One is to sent messages directly to specific services. This I outline walk through as a direct event. Another is to use a broker and a trigger. A broker is basically the place you send stuff to, and then you define specific listeners by creating a trigger.

Read more

Serving a knative function on the root

root to services

I want to deploy everything as a knative service, including the root of the domain. Update: I found an easyier way. Easy way Turn on auto-tls and autocreate-cluster-domain-claims: kubectl patch configmap config-network --namespace knative-serving -p '{"data":{"auto-tls":"Enabled","autocreate-cluster-domain-claims":"true"}}' Then kn domain create gitgratitude.com --ref=homepage That's it. Hardway Left here for the record. Add ingress-nginx helm upgrade --install ingress-nginx ingress-nginx \ --repo https://kubernetes.

Read more