.ace004
.ace004•11mo ago

need to refersh page for message each time a user from different country sends a message

Hey I was going through the below deno chat app and found a bug which is the need to referesh the page each time a person from a different country sends a message how can this be fixed here is the link-https://showcase-chat.deno.dev/
4 Replies
Deno AI Helper
Deno AI Helper•11mo ago
Based on the documentation, the chat application built with Deno uses web sockets for real-time communication. This means that once a client is connected to the server, they should be able to receive messages in real-time without needing to refresh the page. If you're experiencing an issue where you need to refresh the page to see new messages, it could be due to a problem with the web socket connection or the event handlers in the client-side JavaScript code. Here's a snippet of the client-side JavaScript code from the documentation:
// app.js

const myUsername = prompt("Please enter your name") || "Anonymous";
const socket = new WebSocket(
`ws://localhost:8080/start_web_socket?username=${myUsername}`,
);

socket.onmessage = (m) => {
const data = JSON.parse(m.data);

switch (data.event) {
case "update-users":
// refresh displayed user list
let userListHtml = "";
for (const username of data.usernames) {
userListHtml += `<div> ${username} </div>`;
}
document.getElementById("users").innerHTML = userListHtml;
break;

case "send-message":
// display new chat message
addMessage(data.username, data.message);
break;
}
};

function addMessage(username, message) {
// displays new message
document.getElementById(
"conversation",
).innerHTML += `<b> ${username} </b>: ${message} <br/>`;
}
// app.js

const myUsername = prompt("Please enter your name") || "Anonymous";
const socket = new WebSocket(
`ws://localhost:8080/start_web_socket?username=${myUsername}`,
);

socket.onmessage = (m) => {
const data = JSON.parse(m.data);

switch (data.event) {
case "update-users":
// refresh displayed user list
let userListHtml = "";
for (const username of data.usernames) {
userListHtml += `<div> ${username} </div>`;
}
document.getElementById("users").innerHTML = userListHtml;
break;

case "send-message":
// display new chat message
addMessage(data.username, data.message);
break;
}
};

function addMessage(username, message) {
// displays new message
document.getElementById(
"conversation",
).innerHTML += `<b> ${username} </b>: ${message} <br/>`;
}
This code listens for two types of events from the server: "update-users" and "send-message". When a "send-message" event is received, it calls the addMessage function to display the new message. If this isn't working as expected, you might want to check if there are any errors in the browser console that could help you debug the issue. If the issue is related to users in different
Deno AI Helper
Deno AI Helper•11mo ago
countries, it could be due to network latency or other connectivity issues. In this case, you might want to consider using Deno Deploy's BroadcastChannel feature, which is designed to facilitate communication between edge workers in different data center regions, as mentioned in the documentation. However, without more specific information about the issue, it's hard to provide a definitive solution. I would recommend starting with these debugging steps and seeing if they help you identify the problem. 🦕
.ace004
.ace004•11mo ago
this is my code I am already using broadcast api
.ace004
.ace004•11mo ago
More Posts
API architecture for generic callbackHi there, I have a general question about code architecture for an API I'm doing. Basically, it listHow can I run `deno test` on all `*.spec.ts` files contains in a specific directory ?Everything is in the question, it seems very strange that we do not have a `pattern` option to speciuseEffect in the islandsSeeing a strange error in the browser window as soon as I add a simple as ```ts useEffect(() => { Basic react server side rendering does not work on Deno Deploy but does locallyThe below application work correctly locally but fails on Deno Deploy. On Deno Deploy, all my responode cryptoHey everyone, can you explain why certain packages such as node/crypto are not included in newer verPretty new to Deno, what is the recommended way to create a monorepo-like in Deno ?Everything is in the question. I tried during the entire week-end to get up & running with monorepo-[fresh] injecting css into head in a componentWhen I inject css in the head of my html in a component, it works perfectly, but when I reuse the coAutosave firing on editing `.ts` files when a separate Deno enabled project is openHello, I am working on slack automation using deno. I'm developing with webstorm, but I'm having a pProblem with parsing `base64url` from a `Buffer (node)`help , it works in bun but not in Deno, I don't know if is a bug from them or it's not implemented hUUID of managed KV databaseHello, I am trying to follow [this doc](https://docs.deno.com/kv/manual/on_deploy#connect-to-managed