Last active
March 19, 2025 12:42
-
-
Save mortezashojaei/cbd7a0506359a7dae8ae9131bc7e0d9e to your computer and use it in GitHub Desktop.
Using laravel-echo in reactjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { FC } from 'react'; | |
import { useSocket } from '@myapp/hooks'; | |
import {Order} from '@myapp/models'; | |
export const OrdersComponent: FC = () => { | |
const [orders,setOrders] = useState<Order[]>(); | |
function addNewOrder(neworder:Order) { | |
setOrders(prevState => [neworder].concat(prevState)); | |
} | |
useSocket({ | |
type: "ORDER_UPDATED", | |
callBack: (payload: Order) => { | |
addNewOrder(payload) | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from 'react'; | |
import { createSocketConnection } from '@myapp/services'; | |
import { useAppContext } from '@myapp/app-context'; | |
function listen(callBack: (payload: any) => void, channel: string, event: string) { | |
window.Echo.private(channel).listen(event, (payload: any) => { | |
callBack(payload); | |
}); | |
return function cleanUp() { | |
window.Echo.leaveChannel(`private-${channel}`); | |
}; | |
} | |
type Options = { | |
type: 'ORDER_UPDATED' | 'ORDER_UPDATED_NOTICE' | 'NEW_ORDER'; | |
callBack: (payload: any) => void; | |
}; | |
export const useSocket = ({ type, callBack }: Options) => { | |
const [appState] = useAppContext(); | |
useEffect(() => { | |
createSocketConnection(appState.authentication.accessToken); | |
switch (type) { | |
case 'NEW_ORDER': { | |
return listen(callBack, `customer.${appState.user.id}.orders`, '.new_order'); | |
} | |
case 'ORDER_UPDATED': { | |
return listen(callBack, `customer.${appState.user.id}.orders`, '.order_updated'); | |
} | |
case 'ORDER_UPDATED_NOTICE': { | |
return listen(callBack, `customer.${appState.user.id}.notice`, '.order_update_notice'); | |
} | |
} | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Echo from "laravel-echo"; | |
import io from "socket.io-client"; | |
import configs from "@myapp/configs"; | |
declare global { | |
interface Window { | |
io: SocketIOClientStatic; | |
Echo: Echo; | |
} | |
} | |
window.io = io; | |
export function createSocketConnection(token: string) { | |
if (!window.Echo) { | |
window.Echo = new Echo({ | |
broadcaster: "socket.io", | |
host: configs.app.host, | |
transports: ["websocket", "polling", "flashsocket"], | |
auth: { | |
headers: { | |
Authorization: `Bearer ${token}` | |
} | |
} | |
}); | |
} | |
} |
Awesome. Thank you.
Thank you bro
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot for the solution! Saved me a lot of time))
Just need to add to useEffect depends [] (in useSocket).
Thanks man!