Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

faizullah
faizullah

Posted on

     

Laravel-echo and Pushy in React Native Chat app

This documentation is for addinglaravel-echo andPushy in react native to build a chat app.

If we have app that is using laravel backend server and want to implement live chat app in react native than we have to use 4 packages.

pusher-js

laravel-echo

pushy-react-native

@notifee/react-native

Find theinstallation steps see the above links.

After the installation we have to initialise thePusher withkey andoptions. Fill the optionsvalues of your laravel server.

import Pusher from 'pusher-js';

import Echo from 'laravel-echo';

letres:any=awaitgetConfig();lettoken1=res.token;letconfig1=res?.config;letid=res?.id;letoptions:any={cluster:'mt1',key:config1.pusher_app_key,wsHost:config1?.pusher_host,wsPort:config1?.pusher_port,wssPort:config1?.pusher_port,disableStats:true,forceTLS:true,enabledTransports:['wss','ws'],authEndpoint:endpoint1+'/api/broadcasting/auth',auth:{headers:{Authorization:'Bearer'+token1}}};letPusherClient=newPusher(options.key,options);constecho1:any=newEcho({broadcaster:'pusher',client:PusherClient,});if(echo1){constuserChannel1=echo1.private(`messenger.user.${id}`);userChannel1.listen('.new.message',(notification:any)=>{setNotification1(notification);})}
Enter fullscreen modeExit fullscreen mode

Make a method of below code and call it inuseEffect on entry point after login.

Now you will be able to get live chat messages.

Now the problem is for display notification. If your app doesn't use inChina than useFirebase otherwisePushy/TencentCloud is good option. Pushy integration in react native is very easy from Firebase and TencentCloud , but only 100 devices are free.

import notifee from '@notifee/react-native';

Pushy.setNotificationListener(async(data:any)=>{letnotificationTitle=data?.title||'';letnotificationText=data?.message||'Newnotification';constchannelId=awaitnotifee.createChannel({id:'default',name:'DefaultChannel',sound:'default',});awaitnotifee.displayNotification({title:notificationTitle,body:notificationText,android:{channelId,smallIcon:'ic_launcher',pressAction:{id:'default',},},data:data});});notifee.onBackgroundEvent(async()=>{})Pushy.listen();
Enter fullscreen modeExit fullscreen mode

Place the above code just after import statements and before const
App=()=>{} function in App.tsx.

notifee is used to handle notification click event when app is in background and foreground mode.


Conclusion

With these steps, we've successfully integrated React Native withlaravel-echo. Now your app is equipped to leverage instant chat, notification click event in background and foreground.


Final Thoughts
By combining React Native's flexibility with laravel-echo , you've set the foundation for a live chat system, in mobile app. Keep experimenting, and let us know what you build!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am software developer team lead with 6 years experience in react native and reactjs, delivering build on time communicate with product manager on requirements and work other team members .
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp