Skip to content

Planning a request

This section explains how to build and send a trip planning request to the Travel Planner API using WebSocket and Protocol Buffers.
The goal is to send a PlanRequest message, encoded in binary, over the live WebSocket connection.

Use the provided Protobuf definition requestProto from your SDK to generate the message type.

Base endpoint
const reqRoot = protobuf.parse(requestProto).root;
const PlanRequest = reqRoot.lookupType('planner.PlanRequest');

You’ll now build the payload that matches the Protobuf schema.

request.js
const requestObject = {
fromPlace: '52.3702,4.8952', // Amsterdam
toPlace: '52.0907,5.1214', // Utrecht
timestamp: new Date().toISOString(),
arriveBy: false,
userPreferences: {
walkingSpeed: 1, // AVERAGE
bikingSpeed: 1, // AVERAGE
hasEbike: false
}
};

Validation ensures that your request matches the schema before sending it to the server.

main.js
const err = PlanRequest.verify(reqObj);
if (err) throw new Error(err);
const binReq = PlanRequest.encode(PlanRequest.create(reqObj)).finish();
  • verify checks for missing or invalid fields.
  • create constructs an internal Protobuf message instance.
  • encode.finish() serializes the message into a binary Uint8Array.

This binary array is what the WebSocket expects. If you print binReq.length, you’ll see the compact size compared to an equivalent JSON payload.

Once encoded, you can send the message directly over the open WebSocket connection.

main.js
import WebSocket from 'ws';
// Define endpoint
const url = 'wss://api.planner.moopmoop.com/ws/plannermixer/proto';
// Create WebSocket connection with proper headers
const ws = new WebSocket(url, {
headers: { 'Content-Type': 'application/protobuf' }
});
// Handle connection open event
ws.on('open', () => {
console.log(`[open] Connected to Travel Planner API`);
console.log(`[send] Sending PlanRequest (${binReq.length} bytes)`);
ws.send(binReq, { binary: true });
});
// Handle incoming messages
ws.on('message', (data) => {
console.log(`[message] Received ${data.byteLength} bytes`);
// You can decode responses here with your PlanResult Protobuf schema
});
// Handle errors and closure
ws.on('error', (err) => console.error('[error]', err));
ws.on('close', () => console.log('[close] Connection closed'));

You can send multiple PlanRequest in a single connection — for example, when a user changes their destination or adjusts preferences. The server will respond with new PlanResult messages streamed asynchronously.