I keep getting an error when trying to request AIS data from the API could someone please help as I cant figure out why! My code and the error are below.
const WebSocket = require('ws');
const fs = require("fs");
const endpoint = 'wss://stream.aisstream.io/data';
const apiKey = '';
const ws = new WebSocket(endpoint);
ws.onopen = () => {
console.log('Connected to AISstream WebSocket');
I keep getting an error when trying to request AIS data from the API could someone please help as I cant figure out why! My code and the error are below. const WebSocket = require('ws'); const fs = require("fs"); const endpoint = 'wss://stream.aisstream.io/data'; const apiKey = '';
const ws = new WebSocket(endpoint);
ws.onopen = () => { console.log('Connected to AISstream WebSocket');
const subscriptionMessage = JSON.stringify({ APIKey: apiKey, BoundingBoxes: [[[57.283287, -5.757091], [57.361674, -5.624558]]] }); ws.send(subscriptionMessage); };
ws.onmessage = (event) => { const aisData = JSON.parse(event.data); fs.writeFile('ais_data.json', JSON.stringify(aisData, null, 2), (err) => { if (err) { console.error('Error writing JSON file:', err); } else { console.log('AIS data written to ais_data.json'); } }); };
ws.onerror = (error) => { console.error('WebSocket error:', error); console.error('Error details:', error.message, error.stack); };
ws.onclose = () => { console.log('WebSocket connection closed'); };
Error: