---
# System prepended metadata

title: Orders Channel

---

# Orders Channel

```javascript=
import Constants from "../constants/index.js.erb";
import encodedData from "../utils/encodedData";
import logEvent from "../utils/events";

class OrdersChannel {

    constructor(onOrderStatusChange) {
        this.createConsumer();
        this.onOrderStatusChange = onOrderStatusChange;
    }

    createConsumer = () => {
        logEvent('websocket_create_consumer', {channel: 'OrdersChannel'});
        
        this.socket = new WebSocket(`${Constants.foxy_api_action_cable.host}?auth_token=${encodedData.auth_token}`);
        this.socket.onopen = this.subscribe;
        this.socket.onclose = this.onClose;
        this.socket.onmessage = this.onMessage;
        this.socket.onerror = this.onError;
    };

    subscribe = () => {
        logEvent('websocket_subscribe', {channel: 'OrdersChannel'});

        if (!encodedData || !encodedData.auth_token) {
            console.error('Skipping websocket connection, no auth token available');
            return;
        }

        const message = {
            command: 'subscribe',
            identifier: JSON.stringify({
                channel: 'OrdersChannel'
            }),
        };

        this.socket.send(JSON.stringify(message))
    };

    onClose = () => {
        logEvent('websocket_disconnected', {channel: 'OrdersChannel'});

        window.setTimeout(() => {
            logEvent('websocket_reconnect_attempt', {channel: 'OrdersChannel'});
            this.createConsumer();
        }, 5000);
    };

    onMessage = (message) => {
        let data = JSON.parse(message.data);
        // ignore everything but message types
        if (!Object.keys(data).includes('message') || (['ping', 'welcome', 'confirm_subscription'].includes(data.type))) {
            return
        };

        logEvent('websocket_message', {channel: 'OrdersChannel', message: JSON.stringify(data)});

        this.onOrderStatusChange(data.message);
    };

    onError = (data) => {
        console.error(data);
    }
}

export default OrdersChannel;
```

```ruby=

```