marcoeg
marcoeg
WWasp-lang
Created by dbalbo on 9/13/2024 in #đŸ™‹questions
websockets - emit event from server
after setting REACT_APP_API_URL in .env.client it is now attempting to open the WebSocket from the right server. Failing for CORS issues now but should be solved with proper middleware setup.
16 replies
WWasp-lang
Created by dbalbo on 9/13/2024 in #đŸ™‹questions
websockets - emit event from server
even with WASP_SERVER_URL set in .env.server Wasp is attempting to establish a WebSocket connection to localhost.
16 replies
WWasp-lang
Created by dbalbo on 9/13/2024 in #đŸ™‹questions
websockets - emit event from server
trying with setting up WASP_SERVER_URL
16 replies
WWasp-lang
Created by dbalbo on 9/13/2024 in #đŸ™‹questions
websockets - emit event from server
No description
16 replies
WWasp-lang
Created by dbalbo on 9/13/2024 in #đŸ™‹questions
websockets - emit event from server
Yes, that is the correct way to do it. Assigning socket works. Thank you!
16 replies
WWasp-lang
Created by dbalbo on 9/13/2024 in #đŸ™‹questions
websockets - emit event from server
This is very helpful but I still cannot figure out how to send an event to a specific listener. The pattern I would like to implement is very similar, but I would like a client to be able to subscribe to events for the users that is logged in in that client. socket.js:
let ioUserInstance = [];

export const socketFn = (io, context) => {
io.on('connection', (socket) => {
if (Object.keys(socket.data).length !== 0) {
console.log('user connected: ', socket.data.user.id)
ioUserInstance[socket.data.user.id] = io
}
})

io.on('disconnect', () => {
console.log('Client disconnected');
});
};

export const getIoUserInstance = () => ioUserInstance;
let ioUserInstance = [];

export const socketFn = (io, context) => {
io.on('connection', (socket) => {
if (Object.keys(socket.data).length !== 0) {
console.log('user connected: ', socket.data.user.id)
ioUserInstance[socket.data.user.id] = io
}
})

io.on('disconnect', () => {
console.log('Client disconnected');
});
};

export const getIoUserInstance = () => ioUserInstance;
API code:
import { getIoUserInstance } from '../socket.js';

...
const ioUser = getIoUserInstance();
const userId = location.userId;

ioUser[userId].emit('newIncident', newIncident);
import { getIoUserInstance } from '../socket.js';

...
const ioUser = getIoUserInstance();
const userId = location.userId;

ioUser[userId].emit('newIncident', newIncident);
All connected clients receive the event.
16 replies