Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket get all clients #190

Closed
pinqtape opened this issue Sep 29, 2023 · 2 comments
Closed

Websocket get all clients #190

pinqtape opened this issue Sep 29, 2023 · 2 comments

Comments

@pinqtape
Copy link

how can I get all connected users via ws for later changing the data of these users?

@kartikk221
Copy link
Owner

Hi, hyper-express is an unopinionated webserver library hence it does not track the connections internally. However, you can easily implement your own tracking like below:

import crypto from 'crypto';

// We will store connections in this
const connections = new Map();

// Listen for ws connections
server.ws('/connect', (ws) => {
    // Generate a random ID for this connection
    const id = crypto.randomUUID();

    // Track this connection
    connections.set(id, ws);

    // Clean up the connection once disconnected
    ws.once('close', () => {
        // Delete the connection
        connections.delete(id);
    });
});

@pinqtape
Copy link
Author

pinqtape commented Oct 1, 2023

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants