Introduction
Acknowledgment
“An unacknowledged transmission is as good as an unwritten idea—no one knows it ever existed.”
Acknowledgement
An acknowledgment confirms that the server received a message you sent. When acknowledgments are enabled, the client will receive a response message for every sent message that includes an id.
1. Enable Acknowledgment on the Server
Set USE_ACKNOWLEDGMENT
to true
when configuring your ConveyorServer
:
1<?php
2
3include __DIR__ . '/vendor/autoload.php';
4
5use Conveyor\Constants;
6use Conveyor\ConveyorServer;
7
8(new ConveyorServer())
9 ->conveyorOptions([
10 Constants::USE_ACKNOWLEDGMENT => true,
11 ])
12 ->port(8181)
13 ->start();
2. Sending Messages with an ID
To get an acknowledgment, include the key "id"
in your outgoing message. For example:
1const ws = new WebSocket('ws://127.0.0.1:8181/?token=my-temporary-conveyor-token');
2
3ws.onopen = () => {
4 // Connect to the channel
5 ws.send(JSON.stringify({
6 action: 'channel-connect',
7 channel: 'my-channel',
8 id: "f1c7116c-774f-4f3b-945f-dccc00b0106c"
9 }));
10
11 // Broadcast a message to "my-channel"
12 ws.send(JSON.stringify({
13 action: 'broadcast-action',
14 data: 'My broadcast message.',
15 id: "d1c7546c-774f-4f4b-945f-dccc00b0das"
16 }));
17};
3. Receiving the Acknowledgment
If the server successfully receives your message, it sends back something like:
1{
2 "action": "acknowledge-action",
3 "data": "f1c7116c-774f-4f3b-945f-dccc00b0106c"
4}
- action:
"acknowledge-action"
– indicates an acknowledgment. - data: Contains the id of the message you sent.
One important detail is that the Conveyor Client, if set to acknowledge context, will do 2 things:
- It will automatically add id to messages sent to the server with the method
conveyor.send()
. - It will keep the last 20 messages with their respective ack status.
This is useful for debugging and ensuring that messages are being sent and received correctly. That message collection will be available at the conveyor client instance conveyor.messages
property.
Example:
1const connect = (token) => {
2 let conveyor = new window.Conveyor({
3 onMessage: (e) => {
4 console.log(conveyor.messages); // <- Here you can see the messages with their respective ack status
5 output.innerHTML = e;
6 },
7 acknowledge: true,
8 });
9};
10
11document.addEventListener("DOMContentLoaded", () => connect('{{ $token }}'));