Skip to content

Commit

Permalink
Remove SSE
Browse files Browse the repository at this point in the history
  • Loading branch information
GDWR committed Jan 21, 2024
1 parent 1933275 commit 49d76ab
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 37 deletions.
14 changes: 0 additions & 14 deletions components/ShortensCounter.client.vue

This file was deleted.

11 changes: 11 additions & 0 deletions components/ShortensCounter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div class="text-center py-4">
<span v-if="pending">Loading...</span>
<span v-else>Urls Shortened: {{ data?.shortens }}</span>
</div>
</template>


<script setup>
const { pending, data } = useLazyFetch('/api/shortens');
</script>
27 changes: 4 additions & 23 deletions server/api/shortens.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,9 @@ import Redis from 'ioredis';

const runtimeConfig = useRuntimeConfig();
const redis = new Redis({ host: runtimeConfig.redisHost });
const subRedis = new Redis({ host: runtimeConfig.redisHost });

export default defineEventHandler(async (event) => {
setHeader(event, 'cache-control', 'no-cache');
setHeader(event, 'connection', 'keep-alive');
setHeader(event, 'content-type', 'text/event-stream');
setResponseStatus(event, 200);

let counter = 0

const sendEvent = (data: any) => {
event.node.res.write(`id: ${++counter}\n`);
event.node.res.write(`data: ${JSON.stringify(data)}\n\n`);
}

await subRedis.subscribe('shorten');
sendEvent({ shortens: await redis.get(`shorten:count`) });

subRedis.on("message", (_, message) => {
var data = JSON.parse(message);
sendEvent({ shortens: data.shortens });
});

event._handled = true;
})
return {
shortens: await redis.get(`shorten:count`),
};
});

0 comments on commit 49d76ab

Please sign in to comment.