Remote stores powered by Socket.IO with a Vue/Vuex like syntax in the backend.
Server:
import { createStore } from 'tepkijs';
import { Server } from 'socket.io';
const io = new Server(5000, {
cors: {
origin: '*',
}
});
const store = createStore({
io, name: 'room-01',
data: {
messages: [],
_somethingPrivate: {}
},
methods: {
addMessage(message) {
this.messages.push(message);
}
}
});
Client (Svelte):
stores.ts
:
import { createStore } from 'tepkijs-client';
import type { TepkiStore } from 'tepkijs-client';
type State = {
messages: string[]
}
export const store: TepkiStore<State> = createStore('room-01', 'http://localhost:5000');
<script lang="ts">
import { store } from './stores';
let message = '';
function sendMessage() {
store.call('addMessage', message);
message = '';
}
</script>
<main>
{#if $store.isConnected}
<ul>
{#each $store.messages as message}
<li>{message}</li>
{/each}
</ul>
<input bind:value={message}>
<button on:click={sendMessage}>Send</button>
{/if}
</main>