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.
Step 1 - Define Schema
Section titled “Step 1 - Define Schema”Use the provided Protobuf definition requestProto from your SDK to generate the message type.
const reqRoot = protobuf.parse(requestProto).root;const PlanRequest = reqRoot.lookupType('planner.PlanRequest');Step 2 - Create an object
Section titled “Step 2 - Create an object”You’ll now build the payload that matches the Protobuf schema.
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 }};Step 3 - Validate & encode
Section titled “Step 3 - Validate & encode”Validation ensures that your request matches the schema before sending it to the server.
const err = PlanRequest.verify(reqObj);if (err) throw new Error(err);const binReq = PlanRequest.encode(PlanRequest.create(reqObj)).finish();verifychecks for missing or invalid fields.createconstructs 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.
Step 4 - Connect & Send via WebSocket
Section titled “Step 4 - Connect & Send via WebSocket”Once encoded, you can send the message directly over the open WebSocket connection.
import WebSocket from 'ws';
// Define endpointconst url = 'wss://api.planner.moopmoop.com/ws/plannermixer/proto';
// Create WebSocket connection with proper headersconst ws = new WebSocket(url, { headers: { 'Content-Type': 'application/protobuf' }});
// Handle connection open eventws.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 messagesws.on('message', (data) => { console.log(`[message] Received ${data.byteLength} bytes`); // You can decode responses here with your PlanResult Protobuf schema});
// Handle errors and closurews.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.