-
-
Save chrise86/546ee8ff4898beab7fd0cd3e178ae138 to your computer and use it in GitHub Desktop.
simple chat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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