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

Any way to subscript broken connection event? #160

Closed
skyuplam opened this issue Aug 13, 2019 · 4 comments
Closed

Any way to subscript broken connection event? #160

skyuplam opened this issue Aug 13, 2019 · 4 comments
Labels
enhancement New feature or request question Further information is requested scope: websockets Relates to @marblejs/websockets package
Milestone

Comments

@skyuplam
Copy link

Hi there,

Is there any way to known when a websocket client's connection is broken so that I can do a follow-up action, e.g. update the database about the client's status.

@JozefFlakus JozefFlakus added enhancement New feature or request question Further information is requested scope: websockets Relates to @marblejs/websockets package labels Aug 14, 2019
@JozefFlakus JozefFlakus added this to the 3.0 milestone Aug 14, 2019
@JozefFlakus
Copy link
Member

JozefFlakus commented Aug 14, 2019

Hi!

Can you describe how the WebSocket server is bootstrapped? Do you use an intermediate HTTP server and upgrade the connection or you use the WebSocket server directly? Based on that I can help you better.

Right now the @marblejs/websockets module doesn't have "cool" direct access to connected client events, but it will change in the next major release. Right now I can propose you a temporary way of solving this problem :)

@skyuplam
Copy link
Author

skyuplam commented Aug 14, 2019 via email

@JozefFlakus
Copy link
Member

Ok, so the temporary solution consists of reacting to listening server event:

const handleWsServerConnectionEvents = (wsServer: MarbleWebSocketServer) => {
  wsServer.on('connection', conn => {
    // do whatever you want... eg:
    conn.send('hello!');
  });
};

const listening$: HttpServerEffect = (event$, _, { ask }) =>
  event$.pipe(
    matchEvent(ServerEvent.listening),
    map(event => event.payload),
    tap(({ port, host }) => {

      ask(WsServerToken)
        .map(handleWsServerConnectionEvents)
        .getOrElseL(() => { throw new Error('WsServerToken not bound to context'); });

      console.log(`Server running @ http://${host}:${port}/ 🚀`);
    }),
  );

@JozefFlakus
Copy link
Member

Version 3.1 released.

@JozefFlakus JozefFlakus added this to the 3.1 milestone Apr 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested scope: websockets Relates to @marblejs/websockets package
Projects
None yet
Development

No branches or pull requests

2 participants