Deno LandDeno

Deploy Docs

Sockets API

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

addEventListener("fetch", async (event) => {
  // Make a TCP connection to deno.land
  const connection = await Deno.connect({
    port: 80,
    hostname: "deno.land",
  });

  // Send raw HTTP GET request.
  const request = new TextEncoder().encode(
    "GET / HTTP/1.1\nHost: deno.land\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.
  event.respondWith(
    new Response(buffer, {
      headers: {
        "content-type": "text/plain;charset=utf-8",
      },
    }),
  );
});

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

addEventListener("fetch", async (event) => {
  // Make a TLS connection to deno.land
  const connection = await Deno.connectTls({
    port: 443,
    hostname: "deno.land",
  });
  // Send raw HTTP GET request.
  const request = new TextEncoder().encode(
    "GET / HTTP/1.1\nHost: deno.land\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.
  event.respondWith(
    new Response(buffer, {
      headers: {
        "content-type": "text/plain;charset=utf-8",
      },
    }),
  );
});

Please file an issue in our feedback repository if you find any problem in Deploy service or documentation.