Skip to content

Instantly share code, notes, and snippets.

@chrise86
Forked from kryptykphysh/simple-chat.hbs
Created June 25, 2025 14:55
Show Gist options
  • Save chrise86/546ee8ff4898beab7fd0cd3e178ae138 to your computer and use it in GitHub Desktop.
Save chrise86/546ee8ff4898beab7fd0cd3e178ae138 to your computer and use it in GitHub Desktop.
simple chat
{{input value=username}}
{{input value=body}}
<button {{action 'sendMessage'}} data-turbo="false">
Send
</button>
{{#each this.messages as |message|}}
<p>{{message.username}} -> {{message.body}}</p>
{{/each}}
import Component from '@glimmer/component'
import { tracked } from '@glimmer/tracking'
import ActionCable from "actioncable"
/**
* @typedef {Object} AppointmentActionsComponentArgs
* @property {string} username
* @property {string} body
*/
/**
* @typedef {Object} AppointmentActionsComponentSignature
* @property {AppointmentActionsComponentArgs} Args
*/
/**
* @extends {Component<AppointmentActionsComponentSignature>}
*/
export default class AppointmentActionsComponent extends Component {
subscription = null
@tracked messages = []
constructor() {
super(...arguments)
this.#setupSubscription()
}
sendMessage = () => {
this.subscription.send({
username: this.args.username,
body: this.args.body,
})
}
#setupSubscription() {
const consumer = ActionCable.createConsumer("ws://app.rails.localhost:3000/cable")
console.log(consumer)
this.subscription = consumer.subscriptions.create({
channel: 'MessageChannel',
room: 'messages'
}, {
received: (data) => {
console.log(data)
this.messages.push({
username: data.username,
body: data.body,
})
},
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment