Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <header>
  <h1>Simple Chat system</h1>
  <p>Using <a href="https://patchbay.pub/">patchbay.pub</a>
    Chat id: <input type="text" id="chatId"></p>
</header>

<main>
  <ul id="chat"></ul>
</main>
<footer>
  <input type="text" id="username" placeholder="name">
  <input type="text" id="message" placeholder="Message...">
  <button id="send">Send</button>
</footer>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

header {
  text-align: center;
}
body {
  font-family: helvetica, sans-serif;
}
#chatId {
  width: 80px;
}
ul#chat {
  height: 50vh;
  width: min(400px, 90vw);
  margin: 1rem auto 2rem;
  outline: 1px solid #eee;
  box-shadow: 0 2px 12px #0002;
  border-radius: .2rem;
  
  list-style: none;
  padding: 0;
  overflow-y: scroll;
}

ul#chat li {
  padding: 1rem;
}

ul#chat li:nth-child(even) {
  background: #f6f6f6;
}

footer {
  display: flex;
  justify-content: center;
  gap: 1rem;
}
footer #username {
  width: 80px;
}

input {
  padding: .2rem 1rem;
}
              
            
!

JS

              
                const ul = document.querySelector('#chat')
const username = document.querySelector('#username')
const input = document.querySelector('#message')
const sendButton = document.querySelector('#send')
const chatId = document.querySelector('#chatId')

const handleEvtSource = evtSource => {
  evtSource.onmessage = function(e) {
    let data = JSON.parse(e.data)
    let msg = document.createElement('li')
    msg.innerHTML = `<b>${data.author}</b>: ${data.text}`
    ul.appendChild(msg)
    ul.scroll(0,9e9)
  }
}

document.addEventListener('DOMContentLoaded', () => {
  let CHAT_ID = (Math.random() + 1).toString(36).substring(7)
  chatId.value = CHAT_ID

  sendButton.addEventListener('click', () => {
    const message = JSON.stringify({
      author: username.value,
      text: input.value,
    })

    fetch(`https://patchbay.pub/pubsub/${CHAT_ID}`, {
      method: 'POST',
      mode: 'no-cors',
      body: `data: ${message}\n\n`,
    })
    input.value = ''
  })

  let evtSource = new EventSource(`https://patchbay.pub/pubsub/${CHAT_ID}?mime=text%2Fevent-stream&persist=true`)

  chatId.addEventListener('change', e => {
    evtSource.close()
    CHAT_ID = e.target.value
    evtSource = new EventSource(`https://patchbay.pub/pubsub/${CHAT_ID}?mime=text%2Fevent-stream&persist=true`)
    
    handleEvtSource(evtSource)
  })
  
  handleEvtSource(evtSource)

})
              
            
!
999px

Console