---
title: "Write a Custom Transport | Micdrop"
description: "Carry a Micdrop call over anything: the contract of MicdropTransport on the server and MicdropClientTransport on the client, with an in-memory example."
url: "https://micdrop.dev/docs/transports/custom"
---

# Write a Custom Transport

A transport carries a call between `MicdropServer` and its far end. Micdrop ships one for WebSocket, [WebRTC](/docs/transports/webrtc) and [phone lines](/docs/transports/telephony). Write your own to carry a call over another protocol, a provider not covered yet, or a pipe in a test.

## On the server

Extend `MicdropTransport`. The server listens to four events and calls a few methods:

```typescript
import {
  MicdropServerMessage,
  MicdropTransport,
} from '@micdrop/server'

class MyTransport extends MicdropTransport {
  // Whether the far end sends StartSpeaking and StopSpeaking itself
  readonly clientTurns = true

  get isOpen() {
    return this.connection.open
  }

  // A message of the server, which you may encode or drop
  send(message: MicdropServerMessage) {
    this.connection.write(JSON.stringify(message))
  }

  // The assistant voice, PCM16 mono at the rate given
  sendAudio(audio: Buffer, sampleRate: number) {
    this.connection.writeAudio(audio, sampleRate)
  }

  close(code?: number, reason?: string) {
    this.connection.end()
  }
}
```

The transport emits what it receives:

Event

Arguments

When

`Audio`

A `Buffer` of PCM16 mono at 16 kHz

The user speaks, or the stream goes on when the server detects the turns

`Command`

`{ type: 'StartSpeaking' | 'StopSpeaking' | 'Mute' }`

The client found a turn, or paused the call

`Params`

The parameters of the call

They arrive, which `waitForParams` waits for. Call `this.receiveParams(params)`.

`Dtmf`

A digit

A key pressed on a phone

`Close`

A code and a reason

The far end left

A transport whose far end cannot find the turns sets `clientTurns` to false, and the server refuses to start without a [VAD](/docs/server/vad). Three methods are optional:

*   `clearAudio()` drops the voice the far end still holds, called when the user interrupts. A transport that buffers audio on its side needs it.
*   `waitForPlayback()` resolves once the voice sent so far has played, which a hang up waits for.
*   `hangsUp`, set to true, makes the server close the transport itself when the agent ends the call, once the goodbye has played. A client that hears `EndCall` stops on its own, a phone line has nobody to do it.

`encodeServerMessage` and `decodeClientMessage` write and read the text format of the Micdrop protocol, for a transport whose client is `MicdropClient`.

## On the client

Extend `MicdropClientTransport`, and hand a factory to the client as `transport`. It creates one transport per connection attempt, reconnections included:

```typescript
import {
  MicdropClientMessage,
  MicdropClientTransport,
} from '@micdrop/client'

class MyClientTransport extends MicdropClientTransport {
  get isOpen() { /* ... */ }
  get isConnecting() { /* ... */ }

  connect(params?: Record<string, any>) {
    // Open, send the params, then emit Open
  }

  send(message: MicdropClientMessage) { /* a turn command */ }

  // A chunk of the user speech, PCM16 mono at 16 kHz
  sendAudio(pcm: Int16Array) { /* ... */ }

  close() { /* close without emitting Close */ }
}

Micdrop.start({ transport: () => new MyClientTransport() })
```

It emits `Open`, `Message` with a `MicdropServerMessage`, `Audio` with the samples of the assistant and their rate, `Close` with a code and a reason, and `Error`. The client reads the close code as it does for a WebSocket, to show an error and decide whether to reconnect.

A transport that sends the microphone itself, as a media track, sets `sendsMic` to true. The client then sends no chunks, and calls `setMicMuted()` when the microphone should go quiet.

## Example: a call in memory

The test suite of the client connects both sides in memory, with no network, which is the shortest transport that works:

```typescript
class MemoryClientTransport extends MicdropClientTransport {
  serverSide = new MemoryServerTransport(this)
  private open = false

  get isOpen() { return this.open }
  get isConnecting() { return false }

  connect(params?: Record<string, any>) {
    this.open = true
    queueMicrotask(() => {
      this.emit('Open')
      if (params) this.serverSide.setParams(params)
    })
  }

  send(message: MicdropClientMessage) {
    this.serverSide.emit('Command', message)
  }

  sendAudio(pcm: Int16Array) {
    this.serverSide.emit('Audio', Buffer.from(pcm.buffer.slice(0)))
  }

  close() {
    this.open = false
    this.serverSide.emit('Close', 1005, undefined)
  }
}

class MemoryServerTransport extends MicdropTransport {
  readonly clientTurns = true

  constructor(private client: MemoryClientTransport) {
    super()
  }

  get isOpen() { return this.client.isOpen }

  send(message: MicdropServerMessage) {
    this.client.emit('Message', message)
  }

  sendAudio(audio: Buffer, sampleRate: number) {
    const pcm = new Int16Array(audio.buffer.slice(audio.byteOffset, audio.byteOffset + audio.byteLength))
    this.client.emit('Audio', pcm, sampleRate)
  }

  close(code?: number, reason?: string) {
    this.client.emit('Close', { code, reason })
  }

  setParams(params: any) {
    this.receiveParams(params)
  }
}

Micdrop.start({
  transport: () => {
    const transport = new MemoryClientTransport()
    new MicdropServer(transport.serverSide, config)
    return transport
  },
})
```
