This project connects to the Vessel Tracking API's WebSocket interface, subscribes to a vessel, and shows its live positions and the path it has followed on a world map and a close-up map.
It is Part 2 of a two-part series. Part 1, Visualizing Historical Vessel Positions on a World Map with the Vessel Tracking API and Python, replays recent position history from the REST API.
- Python 3.10 or later
- Visual Studio Code with the Jupyter extension (or JupyterLab)
- A Vessel Tracking API key that is entitled to the vessel you want to watch
- Internet access for the OpenStreetMap tiles used by the maps
Install the required packages:
py -m pip install --upgrade pip
py -m pip install -r requirements.txtCreate a config.ini file in the same directory as the notebook:
[vessel_tracking]
api_key = apikey-v1 YOUR_API_KEYDo not commit this file. Add it to .gitignore:
config.ini
.venv/
.ipynb_checkpoints/Open real-time-vessel-tracking-websocket.ipynb, set IMO in step 2 to a vessel you are entitled to see, and run the cells one at a time, in order, up to step 7.
The notebook will:
- Read the vessel's latest position from the REST API.
- Request a single-use ticket and connect to the WebSocket service.
- Subscribe to the vessel and keep the connection alive.
- Draw each position update on a world map and a close-up map.
Notes:
- Do not use Run All. Steps 8 and 9 stop tracking and clear the maps, so they run straight after step 7 starts it.
- Positions arrive only when the vessel reports them, so the first update can take a couple of minutes. The status line above the maps shows how many positions have arrived and when the last message was received.
- Run step 8 to stop tracking and step 9 to clear the maps.
- Before sharing the notebook, clear its outputs because they can contain licensed vessel data.