Skip to content

Instantly share code, notes, and snippets.

@firthous-dev
Last active October 16, 2022 19:04
Show Gist options
  • Select an option

  • Save firthous-dev/59d697d742869282fcf9ad58fb917da4 to your computer and use it in GitHub Desktop.

Select an option

Save firthous-dev/59d697d742869282fcf9ad58fb917da4 to your computer and use it in GitHub Desktop.
<script>
import {publish, subscribe} from './pubsub'
import {onMessageEvent} from './project-events'
import {onMount} from 'svelte'
let message;
//Listen the event onMount and unscubscribe when return.
onMount(()=>{
let onMsg = subscribe(onMessageEvent, (msg)=>{
console.log('Received:', msg); });
return()=>{ onMsg.unsubscribe(); }
});
//Send message using publish event
const onSend = ()=>{
publish(new onMessageEvent({message}));
};
</script>
<input bind:value={message}/>
<button on:click={onSend}>Send</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment