W
Wasp-lang•2mo ago
dbalbo

websockets - emit event from server

Hi guys! I integrated websockets following the guide at https://wasp-lang.dev/docs/advanced/web-sockets. It's not clear to me how to do an emit in an operation (therefore on the server side). For example, at the end of writing a record to the DB, I would like to perform an emit. Thank you
5 Replies
kapa.ai
kapa.ai•2mo ago
Hi @dbalbo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To emit an event from the server side in Wasp, you would typically use the io object within the webSocketFn function. However, currently, Wasp doesn't provide a built-in way to access the io instance outside of the webSocketFn function. A workaround for this issue has been discussed in the Wasp community. Here's the suggested approach: 1. In the file where you have your webSocketFn function, create a "global" variable that you export from that file. Let's call it let ioInstance = undefined. 2. Then set that ioInstance to the io instance you have inside of the webSocketFn function. 3. Elsewhere in your codebase, import the ioInstance and use it. Here's an example of how you might do this:
// In your webSocket.js file
let ioInstance = undefined;

export const webSocketFn = (io, context) => {
io.on('connection', (socket) => {
// Your code here...
})
ioInstance = io;
}

export const getIoInstance = () => ioInstance;
// In your webSocket.js file
let ioInstance = undefined;

export const webSocketFn = (io, context) => {
io.on('connection', (socket) => {
// Your code here...
})
ioInstance = io;
}

export const getIoInstance = () => ioInstance;
Then, in your operation, you can import getIoInstance and use it to emit events:
import { getIoInstance } from './webSocket';

// In your operation...
const io = getIoInstance();
io.emit('yourEvent', yourData);
import { getIoInstance } from './webSocket';

// In your operation...
const io = getIoInstance();
io.emit('yourEvent', yourData);
Please note that this is a workaround and not an official solution. The Wasp team is aware of this limitation and is working on a more integrated solution (source (https://github.com/wasp-lang/wasp/issues/1289)). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @dbalbo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho•2mo ago
The bot gave a good answer. We need to improve this API, but the workaround should work fine. Did you try it?
fustuk.
fustuk.•3w ago
it does, but the docs and maybe the config around sockets need to be updated in the future
miho
miho•3w ago
Yep yep, I agree 100%. There should be something like import { getIoInstance } from 'wasp/server/websocket' and you'd just use that 🙂 + we should also improve the existing docs to mention the workaround!
miho
miho•3w ago
GitHub
Enable using the Websocket server inside of server-side code outsid...
Right now, the initialized server is not exposed or documented for the user to use. For example, when somebody likes a post, it might trigger something and a notification is sent over the socket.
Want results from more Discord servers?
Add your server