Skip to content

Commit

Permalink
feat: ✨ playground code example
Browse files Browse the repository at this point in the history
  • Loading branch information
RihanArfan committed Jun 20, 2024
1 parent 9eacc67 commit b92ebc0
Show file tree
Hide file tree
Showing 10 changed files with 8,087 additions and 0 deletions.
9 changes: 9 additions & 0 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<div>
<!-- TODO: Add PartyKit server & message sending to the playground. -->
Nuxt module playground! Take a look in the plugins/ directory.
</div>
</template>

<script setup>
</script>
3 changes: 3 additions & 0 deletions playground/composables/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function useAccessToken() {
return ref('someAccessToken')
}
4 changes: 4 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default defineNuxtConfig({
modules: ['../src/module'],
devtools: { enabled: true },
})
14 changes: 14 additions & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"private": true,
"name": "partykit-nuxt-playground",
"type": "module",
"scripts": {
"dev": "nuxi dev",
"build": "nuxi build",
"generate": "nuxi generate"
},
"dependencies": {
"nuxt": "^3.12.2",
"valibot": "^0.33.3"
}
}
44 changes: 44 additions & 0 deletions playground/plugins/partykit.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type * as v from 'valibot'

/*
* This example makes partykit available throughout the entirety of the Nuxt application.
* This is useful when designing complete realtime Nuxt application rather than adding some
* real-time to a single page. A simpler approach is just using the `usePartySocket`
* composable directly in the page you want realtime in.
*/
export default defineNuxtPlugin({
name: 'partykit',
parallel: true,
setup() {
const config = useRuntimeConfig()
const accessToken = useAccessToken()

const { hostname } = window.location

const partySocket = usePartySocket<string>({
host: `${hostname}${config.public.partyHost}`,
room: 'hello',
immediate: false,
autoReconnect: true,
query: {
token: accessToken,
},
})

const message = computed((): v.InferOutput<typeof ServerSchemas> | null => {
try {
return JSON.parse(partySocket.data.value || '')
}
catch (error) {
return null
}
})

return {
provide: {
partySocket,
message,
},
}
},
})
28 changes: 28 additions & 0 deletions playground/plugins/realtime-application.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default defineNuxtPlugin({
name: 'realtime-application',
parallel: true,
dependsOn: ['partykit'],
setup() {
const { $message } = useNuxtApp()

watch($message, (event) => {
if (!event?.type) return console.warn('Unknown event received: ', JSON.stringify(event))
const { type, data } = event

console.log('Received event: ', type, data)

switch (type) {
// message
case 'message':
navigateTo('/')
alert('Message received')
break

// unknown event
default:
console.warn('Unknown event received: ', event)
break
}
})
},
})
Loading

0 comments on commit b92ebc0

Please sign in to comment.