Sockets not emitting

Hey all, I'm losing my mind. I recently stopped using socketlib in my convenient effects rewrite, and now I can't get sockets to emit at all. My setup is basically copying how pf2e did it, so I'm a bit confused. And yes, socket: true is set in the manifest. Here's my code (again, mostly derived from pf2e):
Hooks.once("init", () => {
activateSocketListener();
});
Hooks.once("init", () => {
activateSocketListener();
});
function activateSocketListener(): void {
game.socket.on(
SocketEffectHandler.IDENTIFIER,
async (...[message, userId]: SocketEventParams) => {
log(`Socket emitted ${message} ${userId}`);
},
);
}
function activateSocketListener(): void {
game.socket.on(
SocketEffectHandler.IDENTIFIER,
async (...[message, userId]: SocketEventParams) => {
log(`Socket emitted ${message} ${userId}`);
},
);
}
async removeEffect({
effectId,
effectName,
uuid,
origin,
}: IRemoveEffect): Promise<void> {
game.socket.emit(SocketEffectHandler.IDENTIFIER, {
request: "removeEffect",
effectId,
effectName,
uuid,
origin,
} satisfies SocketMessage);
}
async removeEffect({
effectId,
effectName,
uuid,
origin,
}: IRemoveEffect): Promise<void> {
game.socket.emit(SocketEffectHandler.IDENTIFIER, {
request: "removeEffect",
effectId,
effectName,
uuid,
origin,
} satisfies SocketMessage);
}
class SocketEffectHandler {
static IDENTIFIER = `module.${MODULE_ID}`;
// ... omit rest
}
class SocketEffectHandler {
static IDENTIFIER = `module.${MODULE_ID}`;
// ... omit rest
}
Basically I'm never seeing the log come through from the on call
Solution:
Clients don't hear their own socket emissions
Jump to solution
5 Replies
ChaosOS
ChaosOS5mo ago
Where are you checking the socket.on? The same client instance?
Solution
ChaosOS
ChaosOS5mo ago
Clients don't hear their own socket emissions
DFreds
DFredsOP5mo ago
Ohhhh. 😵‍💫 yeah, that was it. So basically... I also need to execute the function locally? I'm now seeing how this worked in socketlib Essentially, it needs to execute as the GM, but the issue was if the GM was the one triggering it. So I should so some check there first to see if I need to emit it at all I guess
ChaosOS
ChaosOS5mo ago
Foundry VTT Community Wiki
Sockets
API documentation for the Socket functionality available to packages.
DFreds
DFredsOP4mo ago
Alright, so basically I wrote this
function emitAsGm(
message: SocketMessage,
handler: () => void | Promise<void>,
): void {
if (game.user.isGM) {
handler(); // execute locally
} else {
if (!game.users.activeGM) {
throw Error();
}

game.socket.emit(SocketEffectHandler.IDENTIFIER, message);
}
}
function emitAsGm(
message: SocketMessage,
handler: () => void | Promise<void>,
): void {
if (game.user.isGM) {
handler(); // execute locally
} else {
if (!game.users.activeGM) {
throw Error();
}

game.socket.emit(SocketEffectHandler.IDENTIFIER, message);
}
}
async removeEffect({
effectId,
effectName,
uuid,
origin,
}: IRemoveEffect): Promise<void> {
emitAsGm(
{
request: "removeEffect",
effectId,
effectName,
uuid,
origin,
} satisfies SocketMessage,
async () => {
const effectHandler = new SocketEffectHandler();
await effectHandler.removeEffect({
effectId,
effectName,
uuid,
origin,
});
},
);
}
async removeEffect({
effectId,
effectName,
uuid,
origin,
}: IRemoveEffect): Promise<void> {
emitAsGm(
{
request: "removeEffect",
effectId,
effectName,
uuid,
origin,
} satisfies SocketMessage,
async () => {
const effectHandler = new SocketEffectHandler();
await effectHandler.removeEffect({
effectId,
effectName,
uuid,
origin,
});
},
);
}
For now anyway, probably could use some refactoring but it works. Thanks for the help!
Want results from more Discord servers?
Add your server