Add socket connection management to ChatWidget and enhance channel authorization in echo.js. Implemented socket status tracking and improved error handling for channel subscriptions, ensuring better reliability in chat interactions.
This commit is contained in:
1 parent
d2196398cc
commit
9e555a8c57
2 files changed
+69
-18
No files matched your search
@@ -45,6 +45,7 @@ function ChatWidget() {
|
|||||||
const [admin, setAdmin] = useState({ available: false, name: null });
|
const [admin, setAdmin] = useState({ available: false, name: null });
|
||||||
const [targetsNext, setTargetsNext] = useState(null);
|
const [targetsNext, setTargetsNext] = useState(null);
|
||||||
const [loadingTargets, setLoadingTargets] = useState(false);
|
const [loadingTargets, setLoadingTargets] = useState(false);
|
||||||
|
const [socketLive, setSocketLive] = useState(false);
|
||||||
|
|
||||||
const listRef = useRef(null);
|
const listRef = useRef(null);
|
||||||
const subscribedId = useRef(null);
|
const subscribedId = useRef(null);
|
||||||
@@ -233,24 +234,40 @@ function ChatWidget() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribedId.current = conversationId;
|
subscribedId.current = conversationId;
|
||||||
echo.private(`chat.${conversationId}`).listen(".message.sent", (payload) => {
|
setSocketLive(false);
|
||||||
if (conversationRef.current?.id !== conversationId || screenRef.current !== "thread") {
|
|
||||||
return;
|
const channel = echo.private(`chat.${conversationId}`);
|
||||||
}
|
channel
|
||||||
appendMessage(payload);
|
.subscribed(() => setSocketLive(true))
|
||||||
if (openRef.current) {
|
.error(() => setSocketLive(false))
|
||||||
markRead(conversationId);
|
.listen(".message.sent", (payload) => {
|
||||||
}
|
if (conversationRef.current?.id !== conversationId || screenRef.current !== "thread") {
|
||||||
});
|
return;
|
||||||
|
}
|
||||||
|
appendMessage(payload);
|
||||||
|
if (openRef.current) {
|
||||||
|
markRead(conversationId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const connection = echo.connector?.pusher?.connection;
|
||||||
|
const markDead = () => setSocketLive(false);
|
||||||
|
connection?.bind("disconnected", markDead);
|
||||||
|
connection?.bind("unavailable", markDead);
|
||||||
|
connection?.bind("failed", markDead);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
connection?.unbind("disconnected", markDead);
|
||||||
|
connection?.unbind("unavailable", markDead);
|
||||||
|
connection?.unbind("failed", markDead);
|
||||||
leaveChatChannel(conversationId);
|
leaveChatChannel(conversationId);
|
||||||
subscribedId.current = null;
|
subscribedId.current = null;
|
||||||
|
setSocketLive(false);
|
||||||
};
|
};
|
||||||
}, [appendMessage, conversation?.id, hidden, markRead, screen]);
|
}, [appendMessage, conversation?.id, hidden, markRead, screen]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!open || screen !== "thread" || !conversation?.id) {
|
if (!open || screen !== "thread" || !conversation?.id || socketLive) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,7 +310,7 @@ function ChatWidget() {
|
|||||||
clearInterval(timer);
|
clearInterval(timer);
|
||||||
document.removeEventListener("visibilitychange", onVisible);
|
document.removeEventListener("visibilitychange", onVisible);
|
||||||
};
|
};
|
||||||
}, [conversation?.id, fetchMessages, open, screen]);
|
}, [conversation?.id, fetchMessages, open, screen, socketLive]);
|
||||||
|
|
||||||
const handleScroll = async () => {
|
const handleScroll = async () => {
|
||||||
if (!listRef.current || !nextPage || !conversation?.id || loadingThread) {
|
if (!listRef.current || !nextPage || !conversation?.id || loadingThread) {
|
||||||
|
|||||||
+41
-7
@@ -53,6 +53,43 @@ function reverbEndpoint() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function authorizeChannel(channel, socketId, callback) {
|
||||||
|
const token = getToken();
|
||||||
|
if (!token) {
|
||||||
|
callback(true, { message: "Missing auth token" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch(apiUrl("broadcasting/auth"), {
|
||||||
|
method: "POST",
|
||||||
|
credentials: "omit",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
Accept: "application/json",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Api-Token": import.meta.env.VITE_API_TOKEN || "hello-mf-s",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
socket_id: socketId,
|
||||||
|
channel_name: channel.name,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
.then(async (response) => {
|
||||||
|
let data = {};
|
||||||
|
try {
|
||||||
|
data = await response.json();
|
||||||
|
} catch {
|
||||||
|
data = {};
|
||||||
|
}
|
||||||
|
if (!response.ok) {
|
||||||
|
callback(true, data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
callback(false, data);
|
||||||
|
})
|
||||||
|
.catch((error) => callback(true, error));
|
||||||
|
}
|
||||||
|
|
||||||
export function getChatEcho() {
|
export function getChatEcho() {
|
||||||
const key = import.meta.env.VITE_REVERB_APP_KEY;
|
const key = import.meta.env.VITE_REVERB_APP_KEY;
|
||||||
const token = getToken();
|
const token = getToken();
|
||||||
@@ -84,15 +121,12 @@ export function getChatEcho() {
|
|||||||
forceTLS: scheme === "https",
|
forceTLS: scheme === "https",
|
||||||
enabledTransports: ["ws", "wss"],
|
enabledTransports: ["ws", "wss"],
|
||||||
disableStats: true,
|
disableStats: true,
|
||||||
|
cluster: "mt1",
|
||||||
Pusher,
|
Pusher,
|
||||||
authEndpoint: apiUrl("broadcasting/auth"),
|
authEndpoint: apiUrl("broadcasting/auth"),
|
||||||
auth: {
|
authorizer: (channel) => ({
|
||||||
headers: {
|
authorize: (socketId, callback) => authorizeChannel(channel, socketId, callback),
|
||||||
Authorization: `Bearer ${token}`,
|
}),
|
||||||
Accept: "application/json",
|
|
||||||
"Api-Token": import.meta.env.VITE_API_TOKEN || "hello-mf-s",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return echoClient;
|
return echoClient;
|
||||||
|
|||||||
Reference in new issue
Block a user