Build Our Shopping Cart Channel

Start building a channel for the shopping cart.

Shopping cart channel

A Channel is a perfect place to store our shopping cart state and to handle user input on the cart. We’ll write a ShoppingCartChannel module that handles these tasks:

  • Adding items
  • Removing items
  • Synchronizing clients

We’ll also add real-time stock updates in the next section.

Remember that Channels are just processes—we’ll use this to our advantage here. Each ShoppingCartChannel represents one open instance of Sneakers23, and the state of the Channel at any time will match what the shopper sees on their page. The Channel is in charge of sending its client the different item details, such as name and availability, for each shoe in the cart.

Let’s start by writing the basic ShoppingCartChannel—we’ll incrementally add more complex features to it throughout this section.

Create the Channel

We’ll use the topic cart:* to connect to our Channel. This topic allows us to identify each connected cart by its ID, which will be useful when we need to synchronize the carts. Let’s start our Channel implementation by adding this definition to the ProductSocket module.

Get hands-on with 1200+ tech skills courses.