socket io with fastify
This commit is contained in:
parent
34e9f8e3ca
commit
0a504a75ce
6 changed files with 101 additions and 47 deletions
|
|
@ -28,6 +28,7 @@
|
|||
"fastify": "^5.6.2",
|
||||
"fastify-cli": "^7.4.1",
|
||||
"fastify-plugin": "^5.1.0",
|
||||
"fastify-socket.io": "^5.1.0",
|
||||
"socket.io": "^4.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ import * as db from '@shared/database';
|
|||
import * as auth from '@shared/auth';
|
||||
import * as swagger from '@shared/swagger';
|
||||
import * as utils from '@shared/utils';
|
||||
import { Server } from 'socket.io';
|
||||
import useSocketIo from 'fastify-socket.io';
|
||||
|
||||
declare const __SERVICE_NAME: string;
|
||||
|
||||
|
|
@ -15,8 +17,12 @@ const routes = import.meta.glob('./routes/**/*.ts', { eager: true });
|
|||
|
||||
// When using .decorate you have to specify added properties for Typescript
|
||||
declare module 'fastify' {
|
||||
export interface FastifyInstance {
|
||||
image_store: string;
|
||||
interface FastifyInstance {
|
||||
io: Server<{
|
||||
hello: (message: string) => string,
|
||||
coucou: (data: { message: string }) => void,
|
||||
message: (msg: string) => void,
|
||||
}>
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -27,6 +33,9 @@ const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
|
|||
await fastify.register(db.useDatabase as FastifyPluginAsync, {});
|
||||
await fastify.register(auth.jwtPlugin as FastifyPluginAsync, {});
|
||||
await fastify.register(auth.authPlugin as FastifyPluginAsync, {});
|
||||
await fastify.register(useSocketIo, {
|
||||
path: '/api/chat/socket.io',
|
||||
});
|
||||
|
||||
// Place here your custom code!
|
||||
for (const plugin of Object.values(plugins)) {
|
||||
|
|
@ -39,6 +48,22 @@ const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
|
|||
void fastify.register(fastifyFormBody, {});
|
||||
void fastify.register(fastifyMultipart, {});
|
||||
fastify.get('/monitoring', () => 'Ok');
|
||||
|
||||
fastify.ready((err) => {
|
||||
if (err) throw err;
|
||||
|
||||
fastify.io.on('connection', (socket) => {
|
||||
console.info('Socket connected!', socket.id);
|
||||
socket.on('hello', (value) => {
|
||||
console.log(`GOT HELLO ${value}`);
|
||||
return 'hi';
|
||||
});
|
||||
socket.on('message', (value) => console.log(`GOT MESSAGE ${value}`));
|
||||
socket.on('coucou', (value) => console.log(`GOT COUCOU ${value.message}`));
|
||||
},
|
||||
);
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
export default app;
|
||||
|
|
|
|||
|
|
@ -1,26 +1,23 @@
|
|||
import { FastifyPluginAsync } from 'fastify';
|
||||
import { MakeStaticResponse, typeResponse } from '@shared/utils';
|
||||
import { Type } from '@sinclair/typebox';
|
||||
import Fastify from 'fastify'
|
||||
import { Server } from "socket.io"
|
||||
import { Socket } from "socket.io";
|
||||
import Fastify from 'fastify';
|
||||
import { Server } from 'socket.io';
|
||||
import { Socket } from 'socket.io';
|
||||
import * as fsocketio from 'fastify-socket.io';
|
||||
|
||||
|
||||
const fastify = Fastify();
|
||||
|
||||
const io = new Server(fastify.server, {
|
||||
path: "/app/chat/socket.io/",
|
||||
cors: { origin: "*" },
|
||||
path: '/app/chat/socket.io/',
|
||||
cors: { origin: '*' },
|
||||
});
|
||||
|
||||
|
||||
|
||||
io.on("connection", (socket: Socket) => {
|
||||
console.log("testing")
|
||||
console.log(`Client connected: ${socket.id}`);
|
||||
socket.on("message", (data: any) => console.log(data, `socketID: ${socket.id}`));
|
||||
socket.once("message", () => socket.send("connected succesfully"));
|
||||
socket.once("coucou", (data: any) => console.log(data))
|
||||
io.on('connection', (socket: Socket) => {
|
||||
console.log('testing');
|
||||
console.log(`Client connected: ${socket.id}`);
|
||||
});
|
||||
|
||||
|
||||
|
|
@ -32,9 +29,24 @@ export const ChatRes = {
|
|||
}),
|
||||
};
|
||||
|
||||
|
||||
export type ChatResType = MakeStaticResponse<typeof ChatRes>;
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify): Promise<void> => {
|
||||
/* await fastify.register(fsocketio.default);
|
||||
|
||||
fastify.get('/api/chat/socket.io', (req, reply) => {
|
||||
console.log('GOT SOCKET ?!');
|
||||
|
||||
const socket = (fastify as any).io;
|
||||
socket.emit('hello');
|
||||
socket.on('message', (data: any) => console.log(data, `socketID: ${socket.id}`));
|
||||
socket.once('message', () => socket.send('connected succesfully'));
|
||||
socket.once('coucou', (data: any) => console.log(data));
|
||||
});
|
||||
*/
|
||||
|
||||
|
||||
fastify.get(
|
||||
'/api/chat/test',
|
||||
{
|
||||
|
|
|
|||
21
src/pnpm-lock.yaml
generated
21
src/pnpm-lock.yaml
generated
|
|
@ -179,6 +179,9 @@ importers:
|
|||
fastify-plugin:
|
||||
specifier: ^5.1.0
|
||||
version: 5.1.0
|
||||
fastify-socket.io:
|
||||
specifier: ^5.1.0
|
||||
version: 5.1.0(fastify@5.6.2)(socket.io@4.8.1)
|
||||
socket.io:
|
||||
specifier: ^4.8.1
|
||||
version: 4.8.1
|
||||
|
|
@ -1788,9 +1791,18 @@ packages:
|
|||
resolution: {integrity: sha512-7Jsfj2uLuGWvnxjrGDrHWpSm65+OcVx0ZbTD2wwkz6Wt6KjGm6+ZYwwpdXdwAlzbJYq+LCEMNvDJc4485AQ1vQ==}
|
||||
hasBin: true
|
||||
|
||||
fastify-plugin@4.5.1:
|
||||
resolution: {integrity: sha512-stRHYGeuqpEZTL1Ef0Ovr2ltazUT9g844X5z/zEBFLG8RYlpDiOCIG+ATvYEp+/zmc7sN29mcIMp8gvYplYPIQ==}
|
||||
|
||||
fastify-plugin@5.1.0:
|
||||
resolution: {integrity: sha512-FAIDA8eovSt5qcDgcBvDuX/v0Cjz0ohGhENZ/wpc3y+oZCY2afZ9Baqql3g/lC+OHRnciQol4ww7tuthOb9idw==}
|
||||
|
||||
fastify-socket.io@5.1.0:
|
||||
resolution: {integrity: sha512-GC1gjrxBGeTbMWV779XHF4uw3AtgKwSQJ9MnjGiMp91ZBuPXEdBYa7NnAMDEl3oZPgK9JO4BlNncTV+UAN+1kg==}
|
||||
peerDependencies:
|
||||
fastify: 4.x.x
|
||||
socket.io: '>=4'
|
||||
|
||||
fastify@5.6.2:
|
||||
resolution: {integrity: sha512-dPugdGnsvYkBlENLhCgX8yhyGCsCPrpA8lFWbTNU428l+YOnLgYHR69hzV8HWPC79n536EqzqQtvhtdaCE0dKg==}
|
||||
|
||||
|
|
@ -4899,8 +4911,17 @@ snapshots:
|
|||
semver: 7.7.3
|
||||
yargs-parser: 22.0.0
|
||||
|
||||
fastify-plugin@4.5.1: {}
|
||||
|
||||
fastify-plugin@5.1.0: {}
|
||||
|
||||
fastify-socket.io@5.1.0(fastify@5.6.2)(socket.io@4.8.1):
|
||||
dependencies:
|
||||
fastify: 5.6.2
|
||||
fastify-plugin: 4.5.1
|
||||
socket.io: 4.8.1
|
||||
tslib: 2.8.1
|
||||
|
||||
fastify@5.6.2:
|
||||
dependencies:
|
||||
'@fastify/ajv-compiler': 4.0.5
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue