saravanabalagi / action-cable-react-jwt

Rails action-cable integration with JWT authentication
MIT License
38 stars 17 forks source link

Support for react native?? #6

Closed maharjanaman closed 5 years ago

maharjanaman commented 5 years ago

Is there a way to implement this in react native???

saravanabalagi commented 5 years ago

Yup, it supports react-native (and I have tested and used it personally for my apps).

And since this is agnostic to whether you use react or react-native, I did not mention specifically in the docs that you can use it with react-native.

createCable = (jwt) => {
    if(this.cable && this.cable.jwt == jwt) return;
    this.cable = ActionCable.createConsumer("ws://localhost:8000/cable", jwt);
    this.subscription = this.cable.subscriptions.create({channel: "OrderChannel"}, {
            connected: ()=> { console.log("cable: connected"); SnackBar.show("Connected", { confirmText:"Dismiss", onConfirm: ()=>SnackBar.dismiss()}); },
            disconnected: ()=> { console.log("cable: disconnected"); SnackBar.show("Disconnected", { confirmText:"Dismiss", onConfirm: ()=>SnackBar.dismiss()}); },
            received: (data) => {
                console.log('yey, received data!');
                SnackBar.show("Received Data!", { confirmText:"Dismiss", onConfirm: ()=>SnackBar.dismiss()});
            }
        }
    )
};

Then you can call createCable(jwt) once you have the JWT. Let me know if you have any problems.