Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
# Description
# Description

This repository contains examples of using the AISStream API in Golang, Python, JavaScript and many more languages.

## Prerequisites

Before you can use these examples, you will need to sign up for an AISStream API key at [aisstream.io](https://aisstream.io/authenticate).

### Golang Example

To run the Golang example, you will need to have Go installed on your system.

Clone the repository: `git clone https://github.com/aisstream/example`. Navigate to the golang directory: `cd example/golang`. Install the dependencies: `go get -d`. Replace "<YOUR API KEY>" in the main.go file with your AISStream API key. Run the example: `go run main.go`.



### Python Example

To run the Python example, you will need to have Python 3 installed on your system.

Clone the repository: `git clone https://github.com/aisstream/example`. Navigate to the python directory: `cd example/python`. Install the dependencies: `python setup.py install`. Replace "<YOUR API KEY>" in the main.py file with your AISStream API key. Run the example: python main.py.

### JavaScript Example

To run the JavaScript example, you will need to have Node.js installed on your system.

Clone the repository: `git clone https://github.com/aisstream/example`. Navigate to the javascript directory: `cd example/javascript`. Install the dependencies: `npm install`. Replace "<YOUR API KEY>" in the index.js file with your AISStream API key.. Run the example: `node index.js`.

### JavaScript Browser Example

Clone the repository: `git clone https://github.com/aisstream/example`. Navigate to the javascript-browser directory: `cd example/javascript-browser`. Run a basic HTTP server, such as: `python -m SimpleHTTPServer`. Replace "<YOUR API KEY>" in the index.js file with your AISStream API key. Open the page `localhost:8000` in your browser.

### Additional Resources

For more information on the AISStream API, visit the [AISStream documentation](https://aisstream.io/documentation).
9 changes: 9 additions & 0 deletions javascript-browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
</head>
<body>
<div id="position-data">
</div>
<script src="index.js"></script>
</body>
</html>
35 changes: 35 additions & 0 deletions javascript-browser/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const container = document.getElementById("position-data");

const socket = new WebSocket("wss://stream.aisstream.io/v0/stream");

socket.onopen = function (event) {
console.log("socket is open", event);
const subscriptionMessage = {
Apikey: "<YOUR API KEY>",
BoundingBoxes: [
[
[-180, -90],
[180, 90],
],
],
};
socket.send(JSON.stringify(subscriptionMessage));
};

socket.onmessage = function (event) {
const aisMessage = JSON.parse(event.data);
if (aisMessage["MessageType"] === "PositionReport") {
let positionReport = aisMessage["Message"]["PositionReport"];
const positionMessage = `ShipId: ${positionReport["UserID"]} Latitude: ${positionReport["Latitude"]} Latitude: ${positionReport["Longitude"]}`;
console.log(positionMessage);
const span = document.createElement("span");
span.innerText = positionMessage;
container.appendChild(span);
}
};

socket.onclose = function (event) {
console.log("socket is closed", event);
};

console.log("created socket", socket);