Hub Methods
Hub methods are invoked from your client application to perform actions on the server. All methods are asynchronous and should be called using connection.invoke().
CreateBasket
Creates a new basket or associates an existing basket ID with your connection.
await connection.invoke("CreateBasket", basketId, salesChannelId);
Parameters
| Parameter | Type | Description |
|---|---|---|
basketId | string | A unique identifier for the basket (UUID recommended) |
salesChannelId | string | Your Sales Channel identifier |
Usage Notes
- Call this method after establishing a connection
- The
basketIdshould be generated client-side and stored in local storage for persistence - If a basket with the given ID already exists, it will be associated with the current connection
Example
function generateBasketId() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
const basketId = localStorage.getItem("basketId") || generateBasketId();
localStorage.setItem("basketId", basketId);
await connection.invoke("CreateBasket", basketId, salesChannelId);
ResumeShopping
Retrieves the current state of an existing basket. Use this to restore the basket after a page reload or reconnection.
const response = await connection.invoke(
"ResumeShopping",
basketId,
salesChannelId,
);
Parameters
| Parameter | Type | Description |
|---|---|---|
basketId | string | The basket identifier to resume |
salesChannelId | string | Your Sales Channel identifier |