# Add a WebSocket Endpoint to a Vapor App from a Linear Issue

Yes. An agent reads the labelled Linear issue, adds a WebSocket route to a Vapor 4 app that pushes new todos to connected clients, tests it and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-websockets/vapor/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the issue and then the app as it is. In a project shaped like vapor/template, `Package.swift` is on swift-tools-version 6.3 with Vapor from 4.121.4, Fluent 4.13 and a Fluent driver, swift-nio 2.101, and a test target on `VaporTesting`, with upcoming features such as `ExistentialAny` and `MemberImportVisibility` enabled for every target. `Sources/App/configure.swift` picks the database from `DATABASE_HOST`, `DATABASE_PORT`, `DATABASE_USERNAME`, `DATABASE_PASSWORD` and `DATABASE_NAME`, adds the `CreateTodo` migration and calls `routes(app)`. `routes.swift` registers `GET /` and `GET /hello` and mounts `TodoController`, whose `boot` groups `GET /todos`, `POST /todos` and `DELETE /todos/:todoID`. `Todo` is a `final class` model with an `@ID` and a `title` field, `TodoDTO` is the `Content` struct the controller decodes and returns, and `Tests/AppTests/AppTests.swift` uses Swift Testing with a `withApp` helper that runs `configure`, `autoMigrate` and `autoRevert` around each case.

The issue asks for a live feed of todos, so the change is one WebSocket route and a small registry of open sockets. The agent adds `Sources/App/TodoFeed.swift`, an actor holding the connected `WebSocket` values keyed by a `UUID`, with `add`, `remove` and a `broadcast(_ dto: TodoDTO)` that sends the encoded DTO as text to each socket and drops the ones whose `send` fails. It stores one instance on the application through a `StorageKey` so the controller and the route share it. In `TodoController.boot` it adds `todos.webSocket("live") { req, ws in … }`, which registers the socket, replies to `ping` text with `pong`, and removes the socket in `onClose`. In `create`, after `todo.save(on: req.db)`, it calls `broadcast` with the DTO it already returns, and in `delete` it broadcasts a small `{"deleted": id}` message so clients can drop the row. Nothing changes in `configure.swift` beyond registering the feed, and the migration is untouched because no column is added.

The tests follow the file that exists. The agent adds cases to `AppTests.swift` inside the same `withApp` helper: one opens a WebSocket to `/todos/live` through the test client, posts a todo to `/todos`, and expects one text frame that decodes to a `TodoDTO` with the posted title; another connects, deletes a todo and expects the `deleted` message; a third sends `ping` and expects `pong`. It runs `swift build` and `swift test` in the sandbox against the database the template's `docker-compose.yml` describes, or against SQLite when the project was generated with `--fluent.db sqlite`, until all cases pass.

Only then does it push a branch and open a pull request that quotes the issue, names the route and the message shapes, and links back to the Linear issue. The template's own `.github/workflows/test-template.yml` builds and tests on `swift:6.3-noble` for every pull request to main, so the new cases run there as well as in the sandbox.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "A Linear issue labelled agent says: \"Add a WebSocket feed for todos. Clients that connect to /todos/live should receive each new todo as JSON when it is created and a deleted message when one is removed, and ping should answer pong.\" Do the work in this repository: add the route in TodoController, keep the open sockets in an actor stored on the Application, broadcast from the existing create and delete handlers, add Swift Testing cases that open the socket through the test client, run swift test, and open a pull request that links back to the issue.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request on its own branch. The diff adds `Sources/App/TodoFeed.swift`, changes `Sources/App/Controllers/TodoController.swift` for the `webSocket` route and the two broadcast calls, adds one line to `Sources/App/configure.swift` to create the feed, and extends `Tests/AppTests/AppTests.swift` with the socket cases. The description quotes the issue, documents the two message shapes a client receives, and links the Linear issue so the tracker shows the pull request.

The checks are the proof. The repository's `test` workflow runs `swift build` and `swift test` in the `swift:6.3-noble` image on the pull request, and the session's trajectory at `GET /o/{org}/sessions/{id}/messages` keeps every `swift test` invocation with its output, so a reviewer sees the socket cases pass rather than reading that they did.

Before merging, a reviewer checks three things: that a socket closed by the client is removed from the actor rather than kept forever, that a failed `send` to one client does not stop the broadcast to the others, and that the route sits under the same path as the REST endpoints so a reverse proxy that forwards `/todos` also upgrades the connection. Everything the agent did is in the diff and the trajectory, and nothing reaches main until the pull request merges.

## Run it on every Linear issue labelled agent

Sign in to Linear once with `POST /me/connections {"type": "app", "name": "linear"}` and finish the `authorize_url` in the console. In the console, add an automation to the Project with this prompt and a `linear` trigger on `linear.issue.labeled`, filtered to the `agent` label; when someone labels an issue the trigger appends the issue after the prompt and starts a run in the Project's repository, and the pull request links back to the issue. An automation run has its own `max_runs_per_hour` cap and is not tied to an API key.

The same request can come from your own server with `POST /o/{org}/sessions`, the repository in `resources` and the issue URL beside it as `https://linear.app/<workspace>/issue/ENG-42`, which puts the issue text in the prompt through your Linear connection. A `request_id` that is a UUID v5 of the issue id keeps a retried call from starting a second run.

## Why run it on Rush?

Picking up this issue on a laptop means a Swift 6.3 toolchain, a Postgres or MySQL container from the template's compose file and a full `swift build` before the first line is written, and the WebSocket tests need the server running inside the test process. On Rush the run does all of that in its own cloud sandbox with its own clone and its own database container, and your machine does nothing.

Labelled issues also arrive in batches. Each one becomes its own sandbox and its own pull request, each with the diff, the Linear link and a trajectory that keeps every `swift test` run with its output. The proof of the change is the change itself, plus the test workflow the repository already has.

## Frequently asked questions

### Does Vapor need an extra package for WebSockets?

No. Vapor 4 ships `app.webSocket` and `routes.webSocket` on top of `websocket-kit`, which `Package.swift` already pulls in through the `vapor` product. The pull request adds no dependency; the only new file is the feed actor.

### How are the open sockets kept across requests?

In an actor, so the Swift 6 strict concurrency checks the template enables pass without `@unchecked Sendable`. One instance is stored on the `Application` through a `StorageKey`, which is how Vapor shares long-lived objects between `configure` and request handlers.

### What does a client receive?

On each `POST /todos`, the same JSON `TodoDTO` the REST response returns, with `id` and `title`. On each `DELETE /todos/:todoID`, a small JSON object with the deleted id. Sending the text `ping` returns `pong`. The pull request description lists these three messages so a frontend can be written against them.

### How are WebSockets tested in Swift Testing?

Through the test client the template already uses: a case opens the socket, performs a REST call in the same `withApp` block, and awaits the frame with a timeout. The cases live in `Tests/AppTests/AppTests.swift` beside the existing todo tests and run with `swift test`.

### Will this work behind my load balancer?

The route is `GET /todos/live` with an `Upgrade` header, so a proxy that forwards `/todos` and allows connection upgrades passes it through. Sticky sessions are a deployment concern the pull request does not touch; with several instances, a client only hears about todos created on the instance it is connected to, and the description says so.

## More Vapor jobs

- [Write Tests for a Vapor Server](https://getrush.ai/run/write-vapor-tests/vapor/)
- [Add Consistent API Error Responses to a Vapor App](https://getrush.ai/run/add-api-error-handling/vapor/)
- [Add API Versioning to a Vapor App](https://getrush.ai/run/add-api-versioning/vapor/)

## The same job on other stacks

- [Add WebSockets to an aiohttp App](https://getrush.ai/run/add-websockets/aiohttp/)
- [Add WebSockets to a Cloudflare Worker with Durable Objects](https://getrush.ai/run/add-websockets/cloudflare-workers/)
- [Add WebSockets to a Fastify App](https://getrush.ai/run/add-websockets/fastify/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
