socket io with fastify

This commit is contained in:
Maieul BOYER 2025-11-18 15:10:32 +01:00 committed by Maix0
parent 34e9f8e3ca
commit 0a504a75ce
6 changed files with 101 additions and 47 deletions

View file

@ -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": {

View file

@ -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;

View file

@ -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',
{