flecks/packages/socket/build/flecks.hooks.js

93 lines
2.4 KiB
JavaScript
Raw Normal View History

export const hooks = {
/**
* Modify Socket.io client configuration.
*
* See: https://socket.io/docs/v4/client-options/
2024-01-29 08:15:22 -06:00
* @invoke MergeAsync
*/
'@flecks/socket.client': () => ({
timeout: Infinity,
}),
2024-01-27 09:16:47 -06:00
/**
* Define server-side intercom channels.
2024-01-29 08:15:22 -06:00
* @invoke Async
*/
'@flecks/socket.intercom': (req) => ({
2024-01-29 08:15:22 -06:00
// Assuming `@my/fleck` implemented this hook, this could be called like:
2024-01-06 16:20:05 -06:00
// `const result = await req.intercom('@my/fleck.key', payload)`.
// `result` will be an `n`-length array, where `n` is the number of server instances. Each
// element in the array will be the result of `someServiceSpecificInformation()` running
// against that server instance.
2024-01-29 08:15:22 -06:00
key: async (payload, server) => {
return someServiceSpecificInformation();
2022-03-07 00:21:16 -06:00
},
}),
2024-01-27 09:16:47 -06:00
/**
2024-01-29 08:15:22 -06:00
* Gather socket packets.
*
2024-01-29 08:15:22 -06:00
* See: [the Gathering guide](../gathering).
* @invoke MergeAsync
*/
'@flecks/socket.packets': Flecks.provide(require.context('./packets', false, /\.js$/)),
2024-01-27 09:16:47 -06:00
/**
2024-01-29 08:15:22 -06:00
* Decorate socket packets.
*
* See: [the Gathering guide](../gathering).
*
* @param {constructor} Packet The packet to decorate.
2024-01-29 08:15:22 -06:00
* @invoke ComposedAsync
*/
'@flecks/socket.packets.decorate': (
Flecks.decorate(require.context('./packets/decorators', false, /\.js$/))
),
/**
* Modify Socket.io server configuration.
*
* See: https://socket.io/docs/v4/server-options/
2024-01-29 08:15:22 -06:00
* @invoke MergeAsync
*/
'@flecks/socket.server': () => ({
pingTimeout: Infinity,
}),
2024-01-27 09:16:47 -06:00
/**
* Do something with a connecting socket.
*
* @param {[ServerSocket](https://github.com/cha0s/flecks/blob/master/packages/socket/src/server/socket.js)} socket The connecting socket.
2024-01-29 08:15:22 -06:00
* @invoke SequentialAsync
2024-01-27 09:16:47 -06:00
*/
'@flecks/socket/server.connect': (socket) => {
socket.on('disconnect', () => {
// ...
});
},
/**
2024-01-29 08:15:22 -06:00
* Do something with the Socket.IO instance.
2024-01-27 09:16:47 -06:00
*
* See: https://socket.io/docs/v4/server-instance/
* @param {SocketIo} io The Socket.IO server instance.
2024-01-29 08:15:22 -06:00
* @invoke SequentialAsync
2024-01-27 09:16:47 -06:00
*/
'@flecks/socket/server.io': (io) => {
io.engine.on("headers", (headers, req) => {
headers["test"] = "789";
});
},
/**
* Define middleware to run when a socket connection is established.
2024-01-29 08:15:22 -06:00
* @invoke Middleware
*/
'@flecks/socket/server.request.socket': () => (socket, next) => {
// Express-style route middleware...
next();
2022-03-07 00:21:16 -06:00
},
2024-01-27 09:16:47 -06:00
2022-03-07 00:21:16 -06:00
};