sevenS
tRPC2y ago
1 reply
seven

sending messages via WebSockets

Hi, do I have the possibility to send messages via wsLink? I want to connect to Binance and be able to place orders via WebSockets, so far I've only been receiving market data like so:
  useEffect(() => {
    const socket = client.binance.marketDataStream.subscribe(
      { symbol },
      {
        onData(data) {
          if (data.type === priceLadderUpdates.Trade) {
            setTradedLevel(data.lastTrade);
          }
          if (data.type === priceLadderUpdates.Depth) {
            setUpdatedLevels([...data.priceLevels]);
          }
        },
      },
    );

    return socket.unsubscribe;
  }, [symbol, setTradedLevel, setUpdatedLevels]);

apart onData there are only onComplete, onError, onSarted, and onStopped.
My mental model is that I would like to broadcast messages with my order params to receive them in my trpc backend and from there transmit this to Binance, is this possible ? I use standalone client
import { createWSClient, splitLink, wsLink, createTRPCClient, httpLink } from '@trpc/client';
import type { AppRouter } from '@api/app';
import superjson from 'superjson';

export const client = createTRPCClient<AppRouter>({
  links: [
    splitLink({
      condition: (op) => op.type === 'subscription',
      true: wsLink({
        client: createWSClient({
          url: `ws://localhost:7000`,
        }),
        transformer: superjson,
      }),
      false: httpLink({
        url: `http://localhost:7000`,
        fetch(url, options) {
          return fetch(url, {
            ...options,
            credentials: 'include',
            cache: 'no-store',
          });
        },
        transformer: superjson,
      }),
    }),
  ],
});

Thanks
Was this page helpful?