Listen to Webhooks
What are Webhooks in LearnCard? Imagine you want your application to know immediately when something interesting happens to a user's LearnCard profile on the network – like receiving a new credential or a connection request. Instead of your app constantly asking "Anything new? Anything new?", the LearnCloud Network can send your application a direct message (a "notification") as soon as that event occurs. This direct message is sent to a specific web address (URL) that you provide, and this mechanism is called a webhook.
What you'll accomplish in this tutorial:
Understand how LearnCloud notifications work at a high level.
Configure a profile on LearnCard to send notifications to your webhook URL.
Build a very simple web server (a "listener") to receive and process these notifications.
See an example of handling a "Connection Request" notification.
Why is this useful?
Webhooks enable you to:
Build responsive applications that react to events in real-time.
Automate workflows based on LearnCloud Network activities.
Enhance user experience by providing timely updates.
Prerequisites:
LearnCard SDK Initialized: An active
learnCard
instance connected to the network (we'll call ityourLearnCardInstance
).A Publicly Accessible URL: Your application needs an endpoint (a URL) that the LearnCloud Network can reach over the internet to send
POST
requests.For local development: Tools like ngrok are perfect for this! Ngrok can create a secure, public URL that tunnels to your local machine. We'll cover this.
For production: This would be a route on your deployed web server or a serverless function (e.g., AWS Lambda + API Gateway, Google Cloud Function).
Basic understanding of:
How webhooks work (HTTP
POST
requests).Node.js and a simple web framework like Express (for our example listener). You can adapt the principles to any backend technology.
Familiarity with LearnCard Profiles: You'll be updating a profile to set its webhook URL.
Part 1: How LearnCloud Notifications Work
When an event occurs (like App
sending a boost to a user whose profile has a webhook configured), the LearnCloud Network API eventually triggers an HTTP POST
request to the registered webhook URL. Your application needs to be listening at that URL.
Part 2: Setting Your Webhook URL in LearnCard
For a LearnCard profile to receive notifications via webhook, you need to tell LearnCard where to send them.
Step 2.1: Get Your Public Webhook URL
If deploying to a server: You'll have a public URL like
https://yourapp.com/api/learncard-webhook
.For Local Development (using ngrok):
If your local listener will run on port 3000 (we'll set this up later), run:
ngrok http 3000
Ngrok will give you a public "Forwarding" URL (e.g.,
https://xxxx-yyy-zzz.ngrok.io
). This is your temporary public webhook URL. Use thehttps
version.
Step 2.2: Update Your LearnCard Profile
Use the LearnCard SDK to update the profile for which you want to receive notifications. Set its notificationsWebhook
field to your public URL.
Action: Run a script with this function call (or integrate it into your app's profile settings) to update the profile you want to receive notifications for.
Part 3: Building a Simple Webhook Listener
Now, let's create a simple server that will listen for incoming notifications at the URL you just configured. We'll use Node.js and Express.
Step 3.1: Project Setup
Step 3.2: Create Your Server (listener.js
or listener.ts
)
listener.js
or listener.ts
)Step 3.3: Running Your Listener
If using JavaScript:
node listener.js
If using TypeScript:
npx ts-node listener.ts
And if you're developing locally, make sure ngrok http 3000
(or your chosen port) is running in another terminal.
Part 4: Triggering and Testing Your Webhook
Now, let's test it! We'll simulate an action that sends a CONNECTION_REQUEST
notification to the profile whose webhook you configured.
Step 4.1: Perform an Action
You'll need another LearnCard instance (let's call it profileA_learnCard
) to act as the requester. The profile you configured with the webhook URL will be profileB_learnCard
(represented by yourLearnCardInstance
in Step 2.2).
Action: Execute code similar to the sendConnectionRequest
function above, where profileA_learnCard
sends a connection request to the profile that has the webhook set up.
Step 4.2: Check Your Listener's Logs
If everything is set up correctly:
Profile A sends the connection request.
LearnCloud Network processes this and identifies that the target profile (Profile B) has a webhook URL.
LearnCloud Network sends a
POST
request to your ngrok URL (which forwards tohttp://localhost:3000/learncard-notifications
).Your
listener.js
/listener.ts
server will receive the request.
You should see output in your listener's console similar to:
Important Considerations
Security: Always use
https
for your webhook URLs in production. Ngrok provides this automatically.Asynchronous Processing: As mentioned, respond with
200 OK
quickly. If you need to do significant processing (like database updates, sending other API calls), do it after sending the response, perhaps by adding the task to an internal queue.Error Handling & Retries: Build robust error handling in your listener. Be aware that LearnCloud might retry sending a notification if it doesn't receive a timely success response. Design your processing to be idempotent (processing the same notification multiple times doesn't cause unintended side effects).
Summary & Next Steps
You've now learned how to:
Configure a webhook URL for a LearnCard profile.
Build a basic webhook listener using Node.js and Express.
Receive and parse an example
CONNECTION_REQUEST
notification.Trigger a test notification.
From here, you can:
Expand your listener to handle various other
type
values from the LearnCloud Network.Integrate more complex business logic into your webhook handler.
Deploy your listener to a robust server or serverless environment for production use.
Happy listening!
Last updated
Was this helpful?