There are two ways to send multiple requests to the head unit: concurrently and sequentially. Which method you should use depends on the type of RPCs being sent. Concurrently sent requests might finish in a random order and should only be used when none of the requests in the group depend on the response of another, such as when subscribing to several hard buttons. Sequentially sent requests only send the next request in the group when a response has been received for the previously sent RPC. Requests should be sent sequentially when you need to know the result of a previous request before sending the next, like when sending the several different requests needed to create a menu.
Both methods can have the await
syntax be used to pause execution until all the responses return, and errors can be caught by attaching a catch
handler. The concurrent method accepts an array of requests and will return an array of responses, while the sequential method accepts an array of requests and returns the last RPC response in the array.
When you send multiple RPCs concurrently, it will not wait for the response of the previous RPC before sending the next one. Therefore, there is no guarantee that responses will be returned in order, and you will not be able to use information sent in a previous RPC for a later RPC.
The JavaScript library concurrent sendRpcs
method will honor the ordering of the requests passed in (the method uses Promise.all behind the scenes). Each response in the array has the same position of their matching request.
// sdl_javascript_suite v1.1+ const subscribeButtonLeft = new SDL.rpc.messages.SubscribeButton() .setButtonName(SDL.rpc.enums.ButtonName.SEEKLEFT); const subscribeButtonRight = new SDL.rpc.messages.SubscribeButton() .setButtonName(SDL.rpc.enums.ButtonName.SEEKRIGHT); const responses = await sdlManager.sendRpcsResolve([subscribeButtonLeft, subscribeButtonRight], (result, messagesRemaining) => { // this is the update callback function }); // thrown exceptions should be caught by a parent function via .catch() // Pre sdl_javascript_suite v1.1 const subscribeButtonLeft = new SDL.rpc.messages.SubscribeButton() .setButtonName(SDL.rpc.enums.ButtonName.SEEKLEFT); const subscribeButtonRight = new SDL.rpc.messages.SubscribeButton() .setButtonName(SDL.rpc.enums.ButtonName.SEEKRIGHT); const responses = await sdlManager.sendRpcs([subscribeButtonLeft, subscribeButtonRight]) .catch(error => { // if an RPC isn't successful, this is invoked with the passed-in failed RPC });
Requests sent sequentially are sent in a set order. The next request is only sent when a response has been received for the previously sent request.
The code example below shows how to create a perform interaction choice set. When creating a perform interaction choice set, the PerformInteraction
RPC can only be sent after the CreateInteractionChoiceSet
RPC has been registered by Core, which is why the requests must be sent sequentially.
// sdl_javascript_suite v1.1+ const choiceId = 111; const choiceSetId = 222; const choice = new SDL.rpc.structs.Choice() .setChoiceID(choiceId) .setMenuName('Choice title'); const createInteractionChoiceSet = new SDL.rpc.messages.CreateInteractionChoiceSet() .setInteractionChoiceSetID(choiceSetId) .setChoiceSet([choice]); const performInteraction = new SDL.rpc.messages.PerformInteraction() .setInitialText('Initial Text') .setInteractionMode(SDL.rpc.enums.InteractionMode.MANUAL_ONLY) .setInteractionChoiceSetIDList([choiceSetId]); const response = await sdlManager.sendSequentialRpcsResolve([createInteractionChoiceSet, performInteraction], (result, messagesRemaining) => { // this is the update callback function }); // thrown exceptions should be caught by a parent function via .catch() // Pre sdl_javascript_suite v1.1 const choiceId = 111; const choiceSetId = 222; const choice = new SDL.rpc.structs.Choice() .setChoiceID(choiceId) .setMenuName('Choice title'); const createInteractionChoiceSet = new SDL.rpc.messages.CreateInteractionChoiceSet() .setInteractionChoiceSetID(choiceSetId) .setChoiceSet([choice]); const performInteraction = new SDL.rpc.messages.PerformInteraction() .setInitialText('Initial Text') .setInteractionMode(SDL.rpc.enums.InteractionMode.MANUAL_ONLY) .setInteractionChoiceSetIDList([choiceSetId]); const response = await sdlManager.sendSequentialRpcs([createInteractionChoiceSet, performInteraction]) .catch(error => { // if an RPC isn't successful, this is invoked with the passed-in failed RPC });