launchscout / phx-live-state

The npm package for LiveState
19 stars 4 forks source link

Can the websocket connection be reused? #3

Open kokjinsam opened 2 months ago

kokjinsam commented 2 months ago

Hello there,

I notice that a new websocket connection is created for each channel. I was just wondering if there's any technical / other reason for not reusing the websocket connection.

superchris commented 2 months ago

The underlying websocket connection is managed for us by Phoenix Channels javascript client, so I'm not sure whether this would be viable or not, TBH. I haven't peeked under the covers in the phoenix code to see whether you could get to the websocket connection there or not, but feel free to have a go and submit a PR if something useful comes of it.

kokjinsam commented 2 months ago

I briefly looked at Phoenix client. I may be wrong but I don't think it handles socket connection pooling. One idea I have is to let user instantiate their own socket instance and remove https://github.com/launchscout/phx-live-state/blob/main/src/LiveState.ts#L89-L92. So changes would be minimal. What do you think?

superchris commented 1 month ago

Hey sorry for slow reply, for some reason GH didn't notify me of your reply :( Anyways, I think letting the user pass in their own instance and creating one for them if they don't would be totally reasonable. That way you can pass one if you have one, if not LiveState will create it as before.