Skip to content

Instantly share code, notes, and snippets.

@chanphiromsok
Last active December 2, 2021 10:15
Show Gist options
  • Save chanphiromsok/0b73a143444102119e0f04e609b61b67 to your computer and use it in GitHub Desktop.
Save chanphiromsok/0b73a143444102119e0f04e609b61b67 to your computer and use it in GitHub Desktop.
defmodule WSSWeb.IntrusmentChannel do
use Phoenix.Channel
def join("instrument_info:" <> trading_symbol, _message, socket) do
IO.puts("Joined topic instrument_info:#{trading_symbol}")
{:ok, socket}
end
def terminate(reason, socket) do
IO.puts("Closing socket #{inspect(reason)} , #{inspect(socket.topic)}")
end
end
useEffect(() => {
let socket = new Socket("ws://localhost:4000/socket");
socket.connect();
let channel = socket.channel(`instrument_info:ETHBTC`, {});
channel
.join()
.receive("ok", (res) => {
console.log("Joined Instrument Successfully", res);
})
.receive("error", (res) => {
console.log("Join Error", res);
});
channel.on("new_ltp", (res) => {
console.log(res);
});
}, []);
defmodule WSSWeb.UserSocket do
use Phoenix.Socket
# Channel
channel "instrument_info:*", WSSWeb.IntrusmentChannel
@impl true
def connect(_params, socket, _connect_info) do
{:ok, socket}
end
@impl true
def id(_socket), do: nil
end
# first arg is topic, sec is event,third is payload send to client
# WSSWeb.Endpoint.broadcast "instrument_info:ETHBTCS","new_ltp",%{"ltp"=> 6.103}
# then client will recieve by sockey.channel("instrument_info:ETHBTC")
# channel.on("new_ltp",(res)=>res) // {ltp: 6.103}
# channel.on("event")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment