-
-
Save arn-ob/81572f9e64c9684a5e09d652c6588f18 to your computer and use it in GitHub Desktop.
Using laravel-echo in reactjs
This file contains 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 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 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}` | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment