Setup SignalR communication about position.
We strongly recommend using the SignalR SDKs .
For negotiation we use HubConnectionBuilder
. The following negotiate endpoint we provide in withUrl
method:
Negotiate endpoint
...
for robust and efficient client-server communication.
To establish a connection, utilize the HubConnectionBuilder
for negotiation as described below:
Negotiate endpoint
Utilize the following endpoint with the withUrl
method of HubConnectionBuilder
:
Code Block |
---|
{baseUrl}/queues/{queueId}/positions/{positionId}?userId=${userId} |
...
Parameters
Parameter | Type | Description |
---|---|---|
userId | guid/uuid | A random GUID/UUID. |
Establishing Connection
Initialize and build the hub connection as shown:
Code Block |
---|
let hubConnectionBuilder = new signalR.HubConnectionBuilder() .withUrl(url, options) // - https://docs.microsoft.com/en-us/aspnet/core/signalr/javascript-client?view=aspnetcore-3.1#reconnect-clients .withAutomaticReconnect({ nextRetryDelayInMilliseconds: (retryContext) => { const retryInSeconds = retryContext.previousRetryCount < 5 ? Math.pow(2, retryContext.previousRetryCount) : maxRetryDelayInSeconds; console.log(`Automatic reconnect retry, reconnecting in: ${retryInSeconds}`); return retryInSeconds * 1000; }, }); const hubConnection = hubConnectionBuilder.build(); const start = () => { makeRetryableFetch(() => { return hubConnection .start() .then(() => console.log('Queue service connection is established')) .catch((err) => { console.error(err); throw err; }); }); }; |
...
Subscription to Position Updates
Once the connection is established you need , subscribe to subscribe for specific hubMethodName. In our case it is updates for the position-update
hub method:
Code Block |
---|
const positionUpdateHubMethodName = subscription.subscribe('position-update' hubConnection.on(positionUpdateHubMethodName, (message: any) => { position }: const { position: } Position }) => {= JSON.parse(message); if (position) { const normalized = normalizePosition(position).entities; ... dispatchPosition({ type: loadPosition.fulfilled.type, payload: normalized }); } }); |
Message Structure
The message from the position-update
contains the following properties:
Code Block |
---|
{ "position": { "id": "5387920d-dfdd-4904-8a51-3f007effcffa", "_ts": 1605613557, "status": "notified", "priority": 0, "location": null, "label": "A3", "customerInfo": [ "Jörgen", "Svensson", "220000191010512345" ], "type": "anonymous", "queue": "c179a3a2-9fd7-4e12-96e7-19d9e957e094", "createdAt": "2020-11-17T11:45:57.114Z", "numberInTheQueue": 6 } } |