Is there a Pug file that can connect to a Websocket and show the incoming values?

Check out this basic pug file snippet:

p Hello

The back end consists of typical components such as node, express, passport, and sql. You can set up a route like this:

app.get('/example', (request, response) => {
  response.render('example.pug');
})

Here's what I want the web page to do:

  1. Establish a connection with a websocket server

  2. Display any incoming text in place of the 'Hello' message

Any suggestions on how to achieve this?

(Just for reference, the server side functions look something like this ... Implementing that part is not an issue.)

function sendInfoToAnyBrowserConnected() {
    connections.forEach(function tell(c) {
        if (c.readyState == WebSocket.OPEN) {
            c.send("this text gets displayed")

What content should be included in my pug file?

Answer №1

I've had to figure it out on my own - curse you, Stack Overflow! It basically involves:

<a id="log" size=10></a>

replacing it with:

var el = document.getElementById("log");

and adding:

el.innerHTML = el.innerHTML + "<br>" + e.data;

That's the gist of it.

So essentially, it consists of:

<html>
<head>
<script type="text/javascript">
    function VanillaData() {
        var ws = new WebSocket("ws://ws.example.com:2345");

        ws.onopen = function() { ws.send("from www ,v1"); };

        ws.onmessage = function(e) {
            el.innerHTML = el.innerHTML + "<br>" + e.data;
        };

        ws.onclose = function() { console.log("ws closed..."); };
    }
</script>
</head>

<body>
Your live stuff:
<br>
<a id="log" size=10></a>
<script type="text/javascript">
    var el = document.getElementById("log");
    VanillaData()
</script>
</body >
</html >

I don't have much knowledge about pug, so I can't provide insight into how the .pug file should be structured.

After using an online html-pug converter, this method proved to be successful:

head
  script(type='text/javascript').
    function VanillaData() {
    var ws = new WebSocket("ws://you.com:2345");
    ws.onopen = function() { ws.send("from www"); };
    ws.onmessage = function(e) {
    el.innerHTML = e.data + "<br>" + el.innerHTML;
    };
    ws.onclose = function() { console.log("ws closed ..."); };
    }
| Live stuff:
br
pre#log(size='10').

script(type='text/javascript').
  var el = document.getElementById("log");
  VanillaData()

It seems to be functioning perfectly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Troubleshooting a CSS Problem in the ADF

I've encountered a serious issue with my ADF Application starting yesterday. Everything was running smoothly until suddenly, I saw this unexpected message in my Jdeveloper Console: < org.apache.myfaces.trinidadinternal.style.util.CSSUtils> < ...

If there are no spaces, text will be removed from my list group

While working on developing a forum, I encountered an issue where if I repeatedly input letters or words without any spaces, it causes everything to overlap. Below is an example highlighting this problem: https://i.sstatic.net/KStNq.png Although there is ...

A single pixel border surrounding an odd number of divs

I've been struggling with a persistent issue and I'm determined to find a solution. My goal is to achieve the following design using css: https://gyazo.com/c8ae39ebc4795027ba7c1067a08d3420 Currently, I have an uneven number of divs styled as fo ...

Is there a concept of negative margin "wrapping"?

I am currently working on the website . I am facing an issue where the left arrow is not appearing in the same position as the right arrow. When I try to mirror the same adjustment I made for the left arrow by using margin-left: -75px, it seems to 'wr ...

Is there a way to verify if text was generated using CSS?

I am working with the following HTML structure: <h4 id="myModalLabel"></h4> In my CSS, I have set the content dynamically using the following code: #myModalLabel::after { content: "gebeurtenis"; } For a live example, you can check out t ...

What is the best way to troubleshoot a peer dependency error in my Angular 10 project while utilizing npm?

I am currently in the process of upgrading my Angular 10 project, which is running on Node 14.x and npm 6.14.x. When I run the following command: npm ls I encounter a series of issues related to peer dependencies in npm. The errors are as follows: "p ...

What steps can I take to troubleshoot issues when creating a React app?

While attempting to set up a react application using npx create-react-app projectreact, I encountered the following error message: PS C:\Users\ahnaa\OneDrive\Documents\Web Developent\Reaact JS> npx create-react-app project ...

Utilize Sass variables to store CSS font-size and line-height properties for seamless styling management

How can I save the font size and line height in a Sass variable, like so: $font-normal: 14px/21px; When using this declaration, I notice that a division occurs as explained in the Sass documentation. Is there a way to prevent this division from happening ...

Include the await keyword within the .then block

I'm trying to execute an await after receiving a response in the .then callback of my code: const info = new getInfo(this.fetchDetails); info .retrieve() .then((res) => { const details = this.getLatestInfo(res, 'John'); }) .ca ...

Issues with Bootstrap's hamburger menu not functioning properly when clicked

Can't seem to get my hamburger button working. I've followed the bootstrap documentation and ensured that Bootstrap and jQuery are in the correct order, but still no luck. Any suggestions on what could be causing this issue? Are my links misplace ...

Is it feasible to use both position absolute and position sticky at the same time? If so, how can this be accomplished?

Is there a way to keep an element fixed on the display without using position: fixed? I want the element to always remain above other elements, so I used z-index. Additionally, I would like the element to be able to move horizontally without scrolling acro ...

execute an npm script from a separate repository

I am looking to run an npm run script from repository A within repository B (both are locally cloned). In repository A, the script is defined in package.json as follows: "scripts": { "my-script": "node my-script.js" } The script can be launched using ...

Addressing see-through box styling in CSS

When working on a website using HTML and CSS, I encountered an issue with a transparent box. All elements within this box are also transparent, but I want them to be solid without any opacity. How can I resolve this? .parent { display: inline-block; ...

Capture an image of the element

Hi there, I'm currently attempting to use PhantomJS to capture a screenshot of a specific element. I'm utilizing the PhantomJS bridge for Node.js: phantom Here's what I have so far: page.includeJs('http://ajax.googleapis.com/ajax/libs ...

Is MongoDb equipped to automatically detect and remove malicious JavaScript code?

I'm currently in the process of developing an application that relies on MongoDB and Node.js. I'm curious to know if MongoDB alone is capable of filtering out any potentially malicious code that may be submitted. For example, can MongoDB filter ...

Invert the motion within a photo carousel

Is there anyone who can assist me with creating a unique photo slider using HTML, CSS, and JS? Currently, it includes various elements such as navigation arrows, dots, and an autoplay function. The timer is reset whenever the arrows or dots are clicked. Ev ...

What is the best way to adjust my column position in order to make the links clickable?

Looking for help with a mobile version website menu bar design. The issue is that the central "+" button is covering the two icons on either side, making them unclickable. Is there a solution to rearrange this layout? Here is the current design: https:// ...

Conventions for naming RESTful URIs for junction tables in databases

Suppose I have three different tables product (id, name) customer (id, name) product_customer (product_id, customer_id) I have already implemented the following REST services (URI'S) for the product and customer entities GET /products => retri ...

Do I need to make any changes to the application when adding a new couchbase node to the cluster

Currently, I am utilizing the Node.js SDK to establish a connection with a couchbase cluster. Despite this, in the Node.js documentation, there is no clear instruction on how to input multiple IP addresses (of cluster nodes) when creating the cluster objec ...

Issue encountered while trying to interpret Bootstrap 4 within Netbeans 8.2

Currently, I am working on an HTML5/JS application using Node.js in Netbeans 8.2. As I develop the welcome page, I intend to incorporate Bootstrap 4. However, when I attempt to add bootstrap.min.css to my stylesheets folder, I encounter an error as shown i ...