---
title: Subscribing to Events
description: Learn how to subscribe to events in the Solana network.
---

Websockets provide an interface where you can listen for certain events. Instead
of pinging a HTTP endpoint at an interval to get frequent updates, you can
instead receive those updates only when they happen.

<CodeTabs storage="cookbook">

```ts !! title="Kit" file=packages/docs-examples/cookbook/development/subscribing-events/kit.ts#region=subscribe highlight=17-19

```

```ts !! title="Legacy" file=packages/docs-examples/cookbook/development/subscribing-events/legacy.ts#region=subscribe highlight=7-14

```

```rust !! title="Rust" file=packages/docs-examples/cookbook/development/subscribing-events/rust/src/main.rs#region=subscribe

```

```py !! title="Python"
import asyncio
from solana.rpc.websocket_api import connect
from solders.keypair import Keypair

async def main():
    keypair = Keypair()

    async with connect("wss://api.devnet.solana.com") as websocket:
        # Subscribe to account changes
        await websocket.account_subscribe(keypair.pubkey())

        # Subscribe to logs
        await websocket.logs_subscribe()

        # Listen for messages
        async for message in websocket:
            print(f"Received: {message}")

if __name__ == "__main__":
    asyncio.run(main())
```

</CodeTabs>
