pusher / pusher-websocket-react-native

React Native official Pusher SDK
MIT License
61 stars 52 forks source link

How do we handle multiple auth endpoints? #126

Closed badalsaibo closed 8 months ago

badalsaibo commented 9 months ago

We have different endpoints for notifications, messages etc.

In the Web we created multiple instance with those endpoints. But the pusher instance in here is a singleton. And adding auth endpoints is only possible in the init stage.

So how do we handle subscribing to different auth endpoints within the same app?

Thanks!

benw-pusher commented 9 months ago

If you require routing to multiple endpoints then it may be useful to implement a load balancer or other device with a central URL that you then route auth requests to based on the request.

Alternatively you can look to implement an https://github.com/pusher/pusher-websocket-react-native?tab=readme-ov-file#onauthorizer onAuthorizer function that allows you to create and manage the auth HTTP request yourself. This would allow you to send the request to the relevant endpoint based on the channel being subscribed to.

badalsaibo commented 9 months ago

I was thinking of doing pusher.init() wherever required, and subscribing therein. Would this cause any issue with other pusher instance within the app?

Like on the initialization of the app I'm subscribing to the notifications channel. Now inside the app, going to messages, he or she will be subscribed to their personal message channel.

In both subscription I'll be doing pusher.init(), just changing the onAuthorizer as you mentioned. Would this cause any issue with the singleton pattern? Will the personal message subscription override the notification subscription I had earlier?

benw-pusher commented 9 months ago

The library is designed to be used as a Singleton so you may see some unwanted behaviour if you create multiple instances. You will also see that multiple instances creates multiple connections, which will consume your concurrent connection count faster.

badalsaibo commented 9 months ago

I have moved forward with the pusher-js/react-native package for now. I hope the team looks into this issue. Maybe have an API similar to the one we have in pusher-js currently.

benw-pusher commented 9 months ago

The Pusher-JS library doesn't support multiple auth endpoints - although it does support user authentication, which is used when calling signIn. User authentication is not supported, only channel authorization. You can read more about it at https://pusher.com/docs/channels/using_channels/user-authentication/ and https://pusher.com/docs/channels/server_api/authorizing-users/.