Skip to main content

TCP sockets and TLS

Deno Deploy supports outbound TCP and TLS connections. These APIs allow you to use databases like PostgreSQL, SQLite, MongoDB, etc., with Deploy.

Deno.connect

Make outbound TCP connections.

The function definition is same as Deno with the limitation that transport option can only be tcp and hostname cannot be localhost or empty.

function Deno.connect(options: ConnectOptions): Promise<Conn>

Example

import { serve } from "https://deno.land/std@0.202.0/http/server.ts";

async function handler(_req) {
// Make a TCP connection to example.com
const connection = await Deno.connect({
port: 80,
hostname: "example.com",
});

// Send raw HTTP GET request.
const request = new TextEncoder().encode(
"GET / HTTP/1.1\nHost: example.com\r\n\r\n",
);
const _bytesWritten = await connection.write(request);

// Read 15 bytes from the connection.
const buffer = new Uint8Array(15);
await connection.read(buffer);
connection.close();

// Return the bytes as plain text.
return new Response(buffer, {
headers: {
"content-type": "text/plain;charset=utf-8",
},
});
}

serve(handler);

Deno.connectTls

Make outbound TLS connections.

The function definition is the same as Deno with the limitation that hostname cannot be localhost or empty.

function Deno.connectTls(options: ConnectTlsOptions): Promise<Conn>

Example

import { serve } from "https://deno.land/std@0.202.0/http/server.ts";

async function handler(_req) {
// Make a TLS connection to example.com
const connection = await Deno.connectTls({
port: 443,
hostname: "example.com",
});

// Send raw HTTP GET request.
const request = new TextEncoder().encode(
"GET / HTTP/1.1\nHost: example.com\r\n\r\n",
);
const _bytesWritten = await connection.write(request);

// Read 15 bytes from the connection.
const buffer = new Uint8Array(15);
await connection.read(buffer);
connection.close();

// Return the bytes as plain text.
return new Response(buffer, {
headers: {
"content-type": "text/plain;charset=utf-8",
},
});
}

serve(handler);