Subscriptions and Streaming
Some operations take up to 30 seconds, because the API has to wait for the train operators' own systems. Over a WebSocket, the API streams results as they arrive, so you can show journeys and prices as they come in instead of a loading spinner.
How streaming works
Streaming runs over a WebSocket connection:
- Subscriptions return several results over time. You subscribe to an operation and get an update each time there is something new.
- Queries and mutations can be sent over the same connection. They return a single result, but without the risk of a connection timeout.
When to use streaming
Use a WebSocket for the operations that can be slow:
- Finding journeys: Journeys are streamed as the routing engine finds them.
- Getting journey offers: Prices arrive as each leg is priced.
- Pricing a round trip: Both directions stream at once, each at its own pace.
- Creating bookings, payments or orders: These don't stream updates, but a WebSocket avoids the risk of a connection timeout.
Fast operations, such as fetching passes, work fine over regular HTTP.
Using subscriptions
The most common subscription is journeyOffer. It sends an update when the journey's legs are known, each time a leg is priced, and when the whole offer is complete. Every update has a status field that shows what is done and what is still loading.
Example: Subscribe to journey offers
import { createClient } from 'graphql-ws'
const client = createClient({
url: 'wss://api-gateway.allaboard.eu',
connectionParams: {
'api-key': 'your-api-key-here',
session: '550e8400-e29b-41d4-a716-446655440000'
}
})
const query = `
subscription JourneyOffer(
$journeyId: ID!
$passengers: [PassengerPlaceholderInput!]!
) {
journeyOffer(journeyId: $journeyId, passengers: $passengers) {
status
itinerary {
__typename
... on SegmentCollection {
status
segments {
departureAt
origin {
name
... on Location {
countryCode
}
}
}
offers {
price {
amount
currency
}
parts {
__typename
... on AdmissionPart {
flexibility
serviceClass
comfortClass
}
... on ReservationPart {
flexibility
comfortClass
accommodation {
type
}
}
}
}
}
... on Stopover {
location {
name
countryCode
}
}
}
}
}
`
const variables = {
journeyId: 'ab3d0a77-742f-4155-af95-64749b0823af',
passengers: [{ type: 'ADULT' }]
}
const subscription = client.iterate({ query, variables })
for await (const data of subscription) {
console.log(data)
// Update your UI with the latest data
}Status updates
The first message has the structure of the journey, but no prices yet:
{
"status": "LOADING",
"itinerary": [
{
"__typename": "SegmentCollection",
"status": "LOADING",
"segments": [
{
"departureAt": "2023-08-01T09:42:00",
"origin": { "name": "Amsterdam", "countryCode": "NL" }
}
]
}
]
}In the next message the first leg is priced, and the other is still loading:
{
"status": "LOADING",
"itinerary": [
{
"__typename": "SegmentCollection",
"status": "SUCCESS",
"segments": [...],
"offers": [
{
"price": { "amount": 13400, "currency": "EUR" },
"parts": [...]
}
]
},
{
"__typename": "SegmentCollection",
"status": "LOADING",
"segments": [...]
}
]
}In the last message every leg is priced, and the offer is complete:
{
"status": "SUCCESS",
"itinerary": [
{
"__typename": "SegmentCollection",
"status": "SUCCESS",
"segments": [...],
"offers": [...]
},
{
"__typename": "SegmentCollection",
"status": "SUCCESS",
"segments": [...],
"offers": [...]
}
]
}Using WebSockets for mutations
These mutations can be slow, so send them over a WebSocket too:
createBookingchecks availability and prices.createOrderpre-books the tickets.createPaymentsets up the hosted payment for an order.
The API itself has no time limit. A regular HTTP request keeps running until it has a result, as long as the client stays connected. But depending on your setup, your own server or HTTP client may time out before that.
Best practices
- Show partial results: Show offers as they arrive, instead of waiting for the last message.
- Show progress: Use the
statusfields to show what is still loading. - Show what failed: If a leg fails, show which legs succeeded and which didn't.