Webhook Relay
PricingLogin
  • Introduction
  • Quick Start - Forwarding
  • Quick Start - Tunnels
  • ๐Ÿ› ๏ธInstallation
    • Relay CLI
      • Install
      • Auto-start
      • Run config.yaml reference
    • Containerized
      • Kubernetes Installation
      • Podman
      • Docker
      • Docker Compose
  • Products
    • ๐Ÿ›ฐ๏ธWebhook Forwarding
      • Glossary
      • WebSocket Server
      • Authentication
      • Custom Domains
    • โšกFunctions
      • Managing functions
      • Edit request/response
      • Working with JSON
      • ๐ŸฆพAdvanced
        • Working with time
        • Testing functions in CLI
        • Making HTTP Requests
        • Multipart Form Data
        • URLEncoded Form Data
        • GCP BigQuery
        • Sending Emails
        • JWT authentication
        • Base64, Hashes, Encryption
      • ๐Ÿค–Integrating into CI/CD
    • ๐Ÿ”ƒTunnels
      • Using tunnels
      • Custom Domains
      • Encryption (HTTPS)
      • Regions
  • ๐Ÿ“Examples
    • Intro to examples
    • Webhooks
      • Receiving webhooks on localhost
      • Receive webhooks inside your JavaScript app
      • Execute shell scripts on remote machines
    • Functions
      • Enrich webhooks from 3rd party APIs
      • Convert DockerHub webhook to Slack notification
      • Allowing only POST requests through
      • Manipulate webhook request body
    • Tunnels
      • Ingress for any Kubernetes environment
      • Demoing your website
    • ๐Ÿ Home Automation
      • Home Assistant
      • Node-RED
      • Raspberry Pi
  • Platform
    • CLI Basics
    • Using CLI Behind Proxy
    • Self-hosting Server
      • Server deployment
      • Client configuration
    • Security & Tech
Powered by GitBook
On this page

Was this helpful?

  1. Examples
  2. Webhooks

Receive webhooks inside your JavaScript app

Learn how to receive and process webhooks directly inside your application.

PreviousReceiving webhooks on localhostNextExecute shell scripts on remote machines

Last updated 2 years ago

Was this helpful?

Webhook Relayโ€™s allows users to receive webhooks inside their application without having public IP, domain or even running a web server themselves. Hereโ€™s a short example application written in JavaScript that subscribes to a stream of webhooks:

// client.js
const WebSocket = require('ws');

const ws = new WebSocket('wss://my.webhookrelay.com/v1/socket');

var apiKey = process.env.RELAY_KEY;
var apiSecret = process.env.RELAY_SECRET;

ws.on('open', function open() {
  // on connection, send our authentication request
  ws.send(JSON.stringify({action: 'auth', key: apiKey, secret: apiSecret}));  
});

ws.on('close', function close() {
  console.log('disconnected');
});

ws.on('message', function incoming(data) {
  console.log(data)
  var msg = JSON.parse(data);
  if (msg.type === 'status' && msg.status === 'authenticated') {
    // if we got authentication confirmation, send subscribe event to the server
    ws.send(JSON.stringify({action: 'subscribe', buckets: ['123']}));
  }
});

To run:

1. Install websocket library ws:

npm i ws

2. Set token key and secret (generate at tokens page):

export RELAY_KEY=your-token-key
export RELAY_SECRET=your-token-secret

3. Start it:

node client.js

Now, if you send a webhook to your public input endpoint, you should see something similar:

$ node client.js
{"type":"status","status":"authenticated","message":"connected successfully, subscribe to buckets"}
{"type":"status","status":"subscribed","message":"subscribed to buckets: 123"}
{"type":"webhook","meta":{"bucked_id":"89e44c32-27ff-4832-8655-8a42d3851b6f","bucket_name":"123","input_id":"ee4ac550-12a4-41a7-837d-dd3356ed1771","input_name":"Default public endpoint"},"headers":{"Content-Length":["15"],"User-Agent":["insomnia/6.0.2"],"Cookie":["__cfduid=dc244a014f0b1e2965544ddb483c3fe1b1525866866"],"Content-Type":["application/json"],"Accept":["*/*"]},"query":"","body":"{\"hi\": \"there\"}","method":"PUT"}
๐Ÿ“
Socket Server