@@ -0,0 +1,230 @@
3+description = "How to work with eyre from outside Urbit."
8+This guide is aimed at developers who have a working knowledge of HTTP grammar (`PUT`, `GET`), and should not require any special knowledge of Urbit's internals. To learn more about a specific application within Urbit, please search for that with the docs once you have finished this guide.
12+You'll need a working development ship to follow along with this guide. It's
13+recommended that you set up a new "fakezod" on port 8080: Run this code,
14+substituting the appropriate path to your Urbit binary:
17+/path/to/urbit -p 8080 -F zod
20+For the unfamiliar, this will create a folder named "zod" in your working directory, from which the ship will run. Use `/path/to/urbit -p 8080 zod` in the future. For more information on this process, see our guide on [Environment Setup](@/docs/development/environment.md).
24+We'll authenticate with a code that can be retrieved from the dojo. You can run
25+`+code` in the dojo to obtain it, but a fakezod's code is always
26+`lidlut-tabwed-pillex-ridrup`.
28+Run this code in your bash terminal (requires cURL):
31+curl -i localhost:8080/~/login -X POST -d "password=lidlut-tabwed-pillex-ridrup"
33+Let's explain what this does:
35+ - `-i` displays the headers, so that you can see the cookie that gets sent
36+ - `localhost:8080/~/login` is the URL to which you are POSTing. You will note this is also the URL you visit when you log into Landscape.
37+ - `-X POST` specifies that you are POSTing data, not GETting.
38+ - `-d "password=lidlut-tabwed-pillex-ridrup"` specifies the code for your Urbit that we retrieved above. It is sent as form data, as if it were sent from an HTML form input field (which it is in Landscape)
40+You should see the following:
44+Date: Sun, 01 Jan 2020 00:00:00 GMT
45+Connection: keep-alive
46+Server: urbit/vere-0.10.8
47+set-cookie: urbauth-~zod=0v3.fvaqc.nnjda.vude1.vb5l6.kmjmg; Path=/; Max-Age=604800
48+transfer-encoding: chunked
51+The `Date` and `set-cookie` will be slightly different of course.
53+Take a look at the `set-cookie` line. This is now your session authentication, and will need to be sent with any future requests. It consists of three parts:
55+ - `urbauth` is a prefix that Eyre uses to check for presence of authentication
56+ - `~zod` is the name of the ship you are logging in as
57+ - `0v3.fvaqc.nnjda.vude1.vb5l6.kmjmg` is the actual authentication token.
59+At present there is no way to separate these parts — you must pass a cookie *as* a cookie. This string must be in the `Cookie` header. The `set-cookie` header is only used for this request upon response. It is what browsers expect when receiving a cookie.
61+## Interacting with Eyre
63+Once authenticated, you will perform all your operations on a channel. You may open as many channels as needed using the same authentication token, but this will increase complexity and memory usage on your Urbit ship.
65+Interacting with Urbit comprises two parts:
67+1. Sending information via PUT requests, which receive HTTP status code `204` (no content) upon success. We will not receive updated information (as in a POST request).
68+2. Receiving data on channels, which must first be subscribed to via an initial request. These come in the form of SSEs ([Server-Sent Events](https://www.w3.org/TR/eventsource/))
70+These can be used independently, i.e. you may choose to only send information or only receive information from your Urbit, based on your application. Both can take place on the same channel.
72+### Creating a Channel
74+For this example, we will use the following channel:
77+http://localhost:8080/~/channel/1601844290-ae45b
80+Let's examine the parts. A channel is simply a URL in this pattern: `{url}/~/channel/{uid}` where `{uid}` is a string of your choosing. Most libraries default to the current unix time plus six hexadecimal characters. Your program should determine a channel for its current session and store that in its state.
82+The channel does not yet exist in Urbit, however. To subscribe to events on this URL, you must first make a request (PUT) to Urbit on this URL. The simplest way to do this is to `hi` ourselves, as if we had entered `|hi our` in the dojo. A `hi` is analogous to a ping, and does not manipulate state. Let's learn how to make requests using this as an example.
86+All requests will take the form of sending a message. Messages can take different forms, such as `subscribe`s, `ack`s, and `poke`s. These are called actions and there are more than these, but these will cover most basic use cases. You can think of them as a somewhat more sophisticated HTTP grammar. A message always requires an `id` and an `action`. Other fields depend on the specific action.
88+Let's look at the structure of a message by examining the most basic `hi` we will send.
92+ 'id': 1, // Required. A sequential ID. Keep track of which messages you have sent.
93+ 'action': 'poke', // Required. The action to take. poke is the most basic way of sending data, like HTTP POST
94+ 'ship': 'zod', // Required by poke. The ship on which to perform the poke. You can only poke foreign ships with JSON, but this is the authenticated ship.
95+ 'app': 'hood', // Required by poke. The Urbit app to which to send the data.
96+ 'mark': 'helm-hi', // Required by poke. The "mark," or type, of data being sent.
97+ 'json': 'Opening airlock' // Required by poke. The actual data being sent.
101+Let's open our channel by sending the following request using cURL (**remember to change the cookie**):
104+curl --header "Content-Type: application/json" \
105+ --cookie "urbauth-~zod=0v3.fvaqc.nnjda.vude1.vb5l6.kmjmg" \
107+ --data '[{"id":1,"action":"poke","ship":"zod","app":"hood","mark":"helm-hi","json":"Opening airlock"}]' \
108+ http://localhost:8080/~/channel/1601844290-ae45b
111+If you switch to your fakezod's dojo, you should see `< ~zod: Opening airlock`.
113+Note that in the JSON that we sent via `--data`, the payload is wrapped in `[]`. This is because we can send multiple messages in a single payload, and Eyre expects a list of items, even if we are only sending one.
115+You will always receive a `204` status code if the `PUT` request successfully reached Urbit, authentication succeeded, and the payload was valid. This is merely an HTTP status code, however. If the request failed within Urbit itself, however, you will receive a "nack" event that you will have to process separately.
117+Let's look at how to receive events from Urbit.
119+### Subscribing to Events
121+Now that Urbit has opened an Eyre channel at our URL, we can watch for events on that URL by sending a `subscribe` message. Let's look at the anatomy of a `susbcribe`:
125+ 'id': 2, // Required. This is the next message we are sending, so we give it the next ID.
126+ 'action': 'subscribe', // Required.
127+ 'ship': 'zod', // Required by subscribe.
128+ 'app': 'chat-view', // Required by subscribe. In this case, we want to watch events that occur in the chat-view app, which is responsible for handling incoming chats.
129+ 'path': '/primary' // Required by subscribe. All apps have different path structures. In the case of chat-view, this is where we see all incoming chats.
133+This is all we need to send Urbit to say "On this URL through which I am sending information, send me SSEs whenever something occurs on this app at this path."
135+You should be able to send this information if you were able to send the above cURL example. However, we now need to be able to listen to the SSEs. The `EventSource` object is available for JavaScript in the browser context, as this is why SSEs were invented, but there should be a library for it in your chosen language. Take a look at a Browser JavaScript example:
138+const eventSource = new EventSource('http://localhost:8080/~/channel/1601844290-ae45b', {
139+ withCredentials: true // Required, sends your cookie
142+eventSource.addEventListener('message', function (event) {
143+ ack(Number(event.lastEventId)); // See section below
144+ const payload = JSON.parse(event.data); // Data is sent in JSON format
145+ payload.id === event.lastEventId; // The SSE spec includes event IDs. This information is duplicated in the payload.
146+ const data = payload.json; // Beyond this, the actual data will vary between apps
149+eventSource.addEventListener('error', function (event) {
155+### Handling Received Events
157+Once you have received an event, it is expected that you will "ack" or "acknowledge" that you have received them. To do so, simply send a message in the following format:
161+ 'id': 3, // Required. Picking up where we left off
162+ 'action': 'ack', // Required.
163+ 'event-id': 15, // Required by ack. Whichever SSE of which you are acknowledging receipt
167+### Unsubscribing from Events
169+You may want to stop receiving events from a particular app path, but you don't want to close the channel altogether yet. Let's look at how to do that. Take a look at the example for subscribing. We sent that message with `id: 2`. We will use that ID to cancel the relevant subscription:
173+ 'id': 4, // Required. We always increment the ID as we send messages.
174+ 'action': 'unsubscribe', // Required.
175+ 'subscription': 2 // Required by unsubscribe. This corresponds to the id of a subscribe message sent earlier.
179+### Deleting a Channel
181+You should clean up your channel when able. To do so, simply send a `delete` message and Eyre will handle the rest so your program can safely exit.
185+ 'id': 5, // Required.
186+ 'action': 'delete' // Required.
192+There are other operations which require authentication but do not require the use of a channel. Some operations are app-specific and will not be enumerated here, but there are two operations that exist across Urbit: `scry` and `spider`.
196+Scrying lets you see what an app has exposed at a particular path. It does not manipulate state.
198+It takes the form `{url}/~/scry/{app}{path}.{mark}`
200+ Let's get the base hash of our fakezod in the same way that Landscape does it.
203+curl --header "Content-Type: application/json" \
204+ --cookie "urbauth-~zod=0v3.fvaqc.nnjda.vude1.vb5l6.kmjmg" \
206+ http://localhost:8080/~/scry/file-server/clay/base/hash.json
209+Note the use of a `GET` request.
211+In this example we're scrying the `file-server` app at the path `/clay/base/hash` and using the `json` mark, which is most common. We receive `"0"` which is correct because we are at the top level of the hierarchy using a fake ship.
213+Scry will fail with a 500 or return a 404 if no data is found.
215+### Running Threads with Spider
217+Running threads is an exception to the rule that we outlined in the section on channels. It uses a `POST` request and both manipulates state and receives information back. It also exposes the ability to send a sequence of commands, i.e. a "thread," hence the name.
219+It takes the form `{url}/spider/{inputMark}/{threadname}/{outputmark}.json`
221+This should be considered an advanced technique, but the basic structure of a request can be seen here:
224+curl --header "Content-Type: application/json" \
225+ --cookie "urbauth-~zod=0v3.fvaqc.nnjda.vude1.vb5l6.kmjmg" \
227+ --data '[{"foo": "bar"}]' \
228+ http://localhost:8080/spider/graph-view-action/graph-create/json.json