# Subscribing & Unsubscribing

Once [connected](https://docs.sfox.com/websocket-api/connecting) to the WebSocket API you can subscribe to various feeds using the `subscribe` or `unsubscribe` commands.&#x20;

These commands should be JSON with the following properties:

<table><thead><tr><th width="116">Property</th><th width="100">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>type</code></td><td>string</td><td>Command you are sending to the WebSocket (<code>subscribe</code> or <code>unsubscribe</code>).</td></tr><tr><td><code>feeds</code></td><td>string[]</td><td>List of the feeds that should be subscribed/unsubscribed to/from, respectively.</td></tr></tbody></table>

### Subscribing

To begin receiving feed messages, you must send a `subscribe` message to the server indicating which feeds to receive. This message is mandatory—you may be disconnected if no `subscribe` has been received.

{% hint style="info" %}
To ensure optimal performance, we suggest maintaining a separate Websocket connection for market data subscriptions ([orderbooks](https://docs.sfox.com/websocket-api/market-data/order-book), [tickers](https://docs.sfox.com/websocket-api/market-data/ticker), [trades](https://docs.sfox.com/websocket-api/market-data/trades)) to ensure high-frequency market data doesn't impact the delivery of your time-sensitive account level updates, such as [balances](https://docs.sfox.com/websocket-api/orders-and-account-data/balances) or [open-orders](https://docs.sfox.com/websocket-api/orders-and-account-data/orders) messages.
{% endhint %}

#### Subscribe Message

```json
// Request -> Subscribe to feed(s)
{
    "type": "subscribe", 
    "feeds": ["<feed 1>", "<feed 2>", ...]
}
```

Once a `subscribe` message is received the server responds with a `subscribe` message that lists all feeds you are subscribed to. Subsequent subscribe messages add to the list of subscriptions.

```json
// Response -> Subscribe request
{
    "type": "success",
    "sequence": 2,
    "timestamp": 1727392002151355776,
    "payload": {
        "action": "subscribe",
        "feeds": [
            "<feed 1>",
            "<feed 2>",
            ...
        ]
    },
    "action": "subscribe"
}
```

#### Example Code

{% tabs %}
{% tab title="NodeJS" %}

```javascript
const subscribeMsg = {
  type: 'subscribe',
  feeds: ['ticker.sfox.btcusd']
}
ws.send(JSON.stringify(subscribeMsg));
```

{% endtab %}

{% tab title="Python" %}

```python
subscribe_msg = {
    "type": "subscribe",
    "feeds": ["ticker.sfox.btcusd"],
}

await ws.send(json.dumps(subscribe_msg))
```

{% endtab %}
{% endtabs %}

### Unsubscribing

To unsubscribe from a feed or feeds, send an `unsubscribe` message. The structure is equivalent to `subscribe` messages.

#### Unsubscribe Message

```json
// Request -> Unsubscribe to feed(s)
{
    "type": "unsubscribe", 
    "feeds": ["<feed 1>", "<feed 2>", ...]
}
```

Once a `unsubscribe` message is received the server responds with an `unsubscribe` message that lists all feeds you are unsubscribed from.

```json
// Response -> Subscribe request
{
    "type": "success",
    "sequence": 4,
    "timestamp": 1727394717752870042,
    "payload": {
        "action": "unsubscribe",
        "feeds": [
            "<feed 1>",
            "<feed 2>",
            ...
        ]
    },
    "action": "unsubscribe"
}
```

#### Example Code

{% tabs %}
{% tab title="NodeJS" %}

```javascript
const unsubscribeMsg = {
  type: 'unsubscribe',
  feeds: ['ticker.sfox.btcusd']
}
ws.send(JSON.stringify(unsubscribeMsg))
```

{% endtab %}

{% tab title="Python" %}

```python
unsubscribe_msg = {
    "type": "unsubscribe",
    "feeds": ["ticker.sfox.btcusd"],
}

await ws.send(json.dumps(unsubscribe_msg))
```

{% endtab %}
{% endtabs %}
