Skip to content

Instantly share code, notes, and snippets.

@mortezashojaei
Last active April 12, 2024 19:57
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mortezashojaei/cbd7a0506359a7dae8ae9131bc7e0d9e to your computer and use it in GitHub Desktop.
Save mortezashojaei/cbd7a0506359a7dae8ae9131bc7e0d9e to your computer and use it in GitHub Desktop.
Using laravel-echo in reactjs
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)
}
});
}
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');
}
}
});
};
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}`
}
}
});
}
}
@alisalmabadi
Copy link

so useful! thanks ๐Ÿ‘ ๐Ÿ’ฏ

@mortezashojaei
Copy link
Author

so useful! thanks

it's my pleasure

@sinamoraddar
Copy link

could be more awesome if we had the whole codebase๐Ÿ˜‚
but anyway, it was a huge help,thanks manโค๐Ÿ˜๐Ÿ‘

@mortezashojaei
Copy link
Author

mortezashojaei commented Apr 23, 2020

could be more awesome if we had the whole codebase
but anyway, it was a huge help,thanks man
Excuse me but i cant share whole base code (because of NDA ) and your welcome, helping you is my pleasure ๐Ÿ˜˜๐Ÿ˜˜๐Ÿ˜˜๐Ÿ˜˜

@MatheusRamalho
Copy link

Hello, I need to create a React project that will work with a laravel api that needs to have real time running.
From what I understand from your code it is possible to install laravel echo and laravel socket.io in react, correct?!
Could you give me the install commands please?

@ahusnullin
Copy link

Thanks a lot for the solution! Saved me a lot of time))
Just need to add to useEffect depends [] (in useSocket).
Thanks man!

@mansha99
Copy link

Awesome. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment