aisstream / issues

7 stars 3 forks source link

I keep getting an error while running your JS code example. #66

Closed poggerschsa closed 5 months ago

poggerschsa commented 6 months ago

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:

AIS error
aisstream commented 5 months ago

It appears your url is wrong. It should be wss://stream.aisstream.io/v0/stream.