drashland/wocket

when closing a channel, send a "closing" message to all clients before closing the channel

crookse opened this issue · 3 comments

when closing a channel, send a "closing" message to all clients before closing the channel

Hi!

I just found this repo and I'd like to help with this issue. It's my first time contributing to an Open Source Deno project. According to the Contributing Guidelines it's a good idea to ask in the issue before coding it.

I also have some questions about the implementation I'll code.

Suppose that I connect to a channel called EgChannel, would you expect a message like this before the channel is closed:

{ "action": "channel_closing", "detail": "{Channel.name} closing" }

This is how it'd look in the wscat client:

$ wscat -c ws://127.0.0.1:1777
> {"connect_to": ["EgChannel"]}
< Connected to EgChannel.
> (...user interactions...)
< (...server responses...)
(...channel is closed...)
< {"from": "Server", "to": "EgChannel", "message": { "action": "channel_closing", "detail": "EgChannel closing"} } 

You think that's OK?

I believe <channel name> is closing might be best, as it relates more to connecting than general message events ie i think it's safe to assume that if the response data is a string, it's a default event. Another reason why is because we cannot assume the data structure a user uses for their messages when sending events, for example, responses i get on the client when i make my web socket servers are:

> { "from": "Server", "to": "<channel>", "message": { "event": "my event eg joined-room", data: "some JSON" } }

But everyone implements it different (like shown above)

But feel free to take a stab at it 👍 Just make sure there are tests to accompany it :)

Example

$ wscat -c ws://127.0.0.1:1777
> {"connect_to": ["EgChannel"]}
< Connected to EgChannel
(...channel is closed...)
< EgChannel is closing

there's a broadcast method in the event emitter class. you might want to leverage that logic. but like ed said, "{channel name} is closing." or maybe even "{channel name} closed." because when a channel closes, it's instantly deleted from the array of channels the server has stored. so closing doesn't really make sense unless there are some checks that happen before the channel is closed, which in this case there are no checks that happen.