Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

How to implement laravel websockets to React Native? #1083

Unanswered
sherlah asked this question in Q&A
Discussion options

Hi, does everyone can help me to implementation laravel websocket to react native?
the new SDK of Pusher for react native does not have option to set ws host.
please help me 🙏

You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

You can set the host using Echo; it works even in React Native. I managed to make it work by passing the Pusher instance in the Echo config object.

import Pusher from 'pusher-js'
import Echo from 'laravel-echo'
export default const EchoInstance = new Echo({
 broadcaster: 'pusher',
 Pusher,
 // your configurations
 wsHost: 'your-host.local',
 wssHost: 'your-host.local',
 wsPort: 6001, // default by laravel-websockets
 wssPort: 6001,
 key: 'your-env-key',
 // ...
})

Then, you can use the instance to subscribe to your channel as you would normally.

You must be logged in to vote
2 replies
Comment options

Thanks for the solution provided. I was able to create the Echo instance with it, but I don't seem to be able to hear any events from the channels. This is my code:

const EchoInstance = new Echo({
 broadcaster: 'pusher',
 Pusher,
 wsHost: 'my host',
 wssHost: 'my host',
 wsPort: 6001,
 wssPort: 6001,
 key: 'my key',
 cluster: 'us2',
});
const channel = EchoInstance.private("private.messages.1");
channel.subscribed(() => {
 console.log("subscribed to channel")
}).listen('.message', (e) => {
 console.log("event fired", e);
})

I got no errors or warnings
Any suggestions?

Comment options

After a lot of errors and tries, I was able to connect to public channels in React Native. This is the code I used:

 useEffect(() => {
 const echo = new Echo({
 broadcaster: 'pusher',
 key: 'pusher-app-key',
 cluster: 'mt1',
 wsHost: 'my-domain.com',
 wsPort: 6001,
 wssPort: 6001,
 encrypted: true,
 forceTLS: false,
 enabledTransports: ['ws', 'wss'],
 authEndpoint: 'https://my-domain.com/broadcasting/auth',
 auth: {
 headers: {
 Authorization: authToken,
 },
 },
 });
 const channel = echo.private(channelUri);
 channel.subscribed(() => {
 console.log("connected");
 }).listen(eventName, (event) => {
 console.log(event);
 }).error((error) => {
 console.log(error);
 });
 }, []);

My problem is that I cant connect to Private channels. I constantly get the error:

{"error": "Unable to retrieve auth string from channel-authorization endpoint - received status: 403 from https://my-domain.com/broadcasting/auth. Clients must be authorized to join private or presence channels. See: https://pusher.com/docs/channels/server_api/authorizing-users/", "status": 403, "type": "AuthError"}

Any help is apreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /