Skip to content

Instantly share code, notes, and snippets.

@drochgenius
Created June 29, 2019 17:31
Show Gist options
  • Save drochgenius/ad4b12a327a488516125e433d7c73a15 to your computer and use it in GitHub Desktop.
Save drochgenius/ad4b12a327a488516125e433d7c73a15 to your computer and use it in GitHub Desktop.
import { css, CSSResult, LitElement, html, customElement, property, TemplateResult } from 'lit-element';
@customElement('kafka-consumer')
export class KafkaConsumer extends LitElement {
@property({ type: Array })
private message: string;
public static get styles(): CSSResult {
return css`
div {
border: 2px solid black;
padding: 1em;
font-size: 1.5em;
color: blue;
background-color: lightyellow;
}
`;
}
public constructor() {
super();
this.socketConnect();
}
protected render(): TemplateResult {
return html`
<div>
<p>${this.message}</p>
</div>
`;
}
private socketConnect(): void {
const ws = new WebSocket('ws://localhost:3210', ['json']);
ws.addEventListener(
'open',
(): void => {
console.log('websocket connection open');
}
);
// Listen to messages coming from the server over the Web Socket
ws.addEventListener('message', this.handleMessage.bind(this));
}
private handleMessage(event: MessageEvent): void {
console.log('Received:', event.data);
this.message = event.data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment