Skip to content
Writing
CloudflareHonoEdgeServerless

Cloudflare Workers & Hono at the Edge: Ultra-Low Latency Email Dispatch

Execute email dispatch within 5ms of your users globally by deploying lightweight Hono applications to Cloudflare Workers.

Why Edge Computing Revolutionized Transactional Email

Traditional centralized backends (e.g. AWS us-east-1) force global mobile users in Tokyo, London, or Sydney to suffer 200–350ms round-trip latency simply to submit an alert or password reset. Cloudflare Workers executes JavaScript and TypeScript within 300+ cities globally, terminating TLS within single-digit milliseconds of the client.

By pairing the lightweight Hono web framework (<14kB) with SadaSend Anycast edge ingestion endpoints, you create an edge dispatch gateway capable of validating requests, verifying HMAC webhook signatures, and firing transactional emails with low-latency server overhead.

1. Why Edge Runtimes Abandoned Direct SMTP Sockets

V8 isolates (the underlying engine powering Cloudflare Workers, Deno Deploy, and Vercel Edge) intentionally omit the legacy Node.js net.Socket and tls.TLSSocket APIs. Attempting to run classic SMTP drivers like Nodemailer results in fatal compilation failures.

Even where socket polyfills exist (cloudflare:sockets), opening raw TCP connections on port 587 introduces TLS negotiation delays and connection teardown penalties. Native web-standard fetch() over HTTPS port 443 operates without socket friction, benefiting from edge connection reuse and Cloudflare Anycast routing.

2. Complete Edge Gateway Implementation (src/index.ts)

Here is the complete production Hono gateway. It enforces bearer authentication on your microservice, validates payloads, and routes alerts to SadaSend.

TYPESCRIPT
import { Hono } from 'hono';
import { bearerAuth } from 'hono/bearer-auth';
import type { StatusCode } from 'hono/utils/http-status';

type Bindings = {
  SADASEND_API_KEY: string;
  GATEWAY_SECRET: string;
};

const app = new Hono<{ Bindings: Bindings }>();

// Protect internal gateway endpoint
app.use('/v1/*', (c, next) => {
  const auth = bearerAuth({ token: c.env.GATEWAY_SECRET });
  return auth(c, next);
});

app.post('/v1/dispatch-alert', async (c) => {
  const { recipient, title, details, priority } = await c.req.json<{
    recipient: string;
    title: string;
    details: string;
    priority?: 'low' | 'high';
  }>();

  if (!recipient || !title || !details) {
    return c.json({ error: 'Missing required parameters: recipient, title, details' }, 400);
  }

  const startTime = Date.now();

  const res = await fetch('https://api.sadasend.com/emails', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${c.env.SADASEND_API_KEY}`,
      'Content-Type': 'application/json',
      'Idempotency-Key': `alert-${recipient}-${Date.now()}`,
    },
    body: JSON.stringify({
      to: recipient,
      subject: `[${(priority || 'normal').toUpperCase()}] ${title}`,
      text: details,
    }),
  });

  const latencyMs = Date.now() - startTime;
  const result = await res.json();

  if (!res.ok) {
    return c.json(
      { error: result.message || 'Dispatch failed', upstreamStatus: res.status },
      res.status as StatusCode
    );
  }

  return c.json({
    success: true,
    messageId: result.id,
    edgeLatencyMs: latencyMs,
  });
});

export default app;

3. Edge Deployment & Production Secret Management

Deploying to Cloudflare global network requires zero server configuration. Secrets are securely encrypted in Cloudflare Key Management and injected into runtime environment bindings.

BASH
# 1. Provision Cloudflare Worker secrets
npx wrangler secret put SADASEND_API_KEY
npx wrangler secret put GATEWAY_SECRET

# 2. Deploy globally across 300+ cities in < 5 seconds
npx wrangler deploy
Free plan

Building AI agents that send email?

Scoped API keys, per-key recipient allowlists, approval mode and a hosted MCP server with ten tools — on the free plan, without a card.