This script is a simple example of how to get a continuous data feed from Sync Gateway / App Services like IMAGE BELOW.
For a complete list of options to the _changes api go to: https://developer.couchbase.com/documentation/mobile/current/references/sync-gateway/rest-api/index.html#/database
Clone or download the repository:
git clone https://github.com/Fujio-Turner/sg-stream-demo.git
cd sg-stream-demoOr download the ZIP file from GitHub and extract it, then navigate to the extracted folder.
Open Terminal:
- On Mac: Open Terminal application (Applications > Utilities > Terminal) or press
Cmd + Spaceand type "Terminal" - On Windows: Open Command Prompt or PowerShell by pressing
Win + R, typingcmdorpowershell, and pressing Enter
Navigate to the project directory:
cd path/to/sg-stream-demoOn Mac/Linux:
python3 -m venv venv
source venv/bin/activateOn Windows:
python -m venv venv
venv\Scripts\activatepip install -r requirements.txtOpen sg_feed.py and update the following parameters:
secure = False # Set to True for https (ALWAYS True for Couchbase Capella)
host = "localhost" # Your Sync Gateway/App Services host
port = "4984" # Your Sync Gateway/App Services port
sgDb = "db" # Your database name
sgScope = "us" # Your scope name
sgCollection = "prices" # Your collection name
userName = "bob" # Your username
password = "password" # Your passwordNote: When connecting to Couchbase Capella, you must set secure = True to use HTTPS.
Optional: Uncomment the channel filter section to filter by specific channels:
channels = 'bob,water,cake'
url_param = url_param +'&filter=sync_gateway/bychannel&channels='+channelsOpen sg_websocket_feed.py and update the following parameters:
secure = False # Set to True for wss (ALWAYS True for Couchbase Capella)
host = "localhost" # Your Sync Gateway/App Services host
sgPort = "4984" # Your Sync Gateway/App Services port
sgDb = "db" # Your database name
sgScope = "us" # Your scope name
sgCollection = "prices" # Your collection name
username = "bob" # Your username
password = "password" # Your passwordNote: When connecting to Couchbase Capella, you must set secure = True to use WSS (WebSocket Secure).
Optional: Uncomment the channel filter in the on_open function to filter by specific channels:
channels = ["bob", "water", "cake"]
payload["filter"] = "sync_gateway/bychannel"
payload["channels"] = channelspython sg_feed.pypython sg_websocket_feed.pyMake changes to your Sync Gateway database and watch the continuous feed display the changes in real-time.
Press Ctrl+C to stop the script.
Unlike Couchbase Lite, which can open and sync multiple collections at once, these HTTP and WebSocket change feed methods require a separate connection for each collection you want to monitor.
