MatthewWid / better-sse

⬆ Dead simple, dependency-less, spec-compliant server-sent events implementation for Node, written in TypeScript.
MIT License
485 stars 14 forks source link

[QUESTION] - Session Metadata & Disconnection #58

Closed go4cas closed 4 months ago

go4cas commented 6 months ago

I'm really keen on using this library in a real-time dashboard stack, using unjs' Nitro server. I just a few questions (which I could not find answers for in the docs):

  1. I would ideally like to add some metadata to each connection (Session). This could include data like user-agent, user identifier (internal ID, email, IP address, etc), connection timestamp, etc. Is this supported at the moment?
  2. I intend to include an Admin module in my app, whereby connected clients will be listed, by channel. I would prefer to have the feature to manually disconnect a client. is this available?
MatthewWid commented 6 months ago
  1. Yes. You can use the Session#state property to add metadata to sessions (as well as channels).

    If you're using TypeScript, you can add type information to the metadata object via generics when you create the session:

    const session = createSession<{email: string}>();
    
    session.state.email = "example@gmail.com";
    
    session.state.email = 12345; // TypeScript Error: Type 'number' is not assignable to type 'string'.

    Or you can change the default session state for all sessions using module augmentation and declaration merging and adding properties to the DefaultSessionState interface.

  2. See #47.