Created
June 22, 2024 11:22
-
-
Save chinel/5b8e47ee010bcf7ff78492c622efc13f to your computer and use it in GitHub Desktop.
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, { useEffect } from "react"; | |
| import { Centrifuge } from "centrifuge"; | |
| const Orderbook = () => { | |
| const websocketUrl = "wss://api.testnet.rabbitx.io/ws"; | |
| const websocketToken = | |
| "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIwIiwiZXhwIjo1MjYyNjUyMDEwfQ.x_245iYDEvTTbraw1gt4jmFRFfgMJb-GJ-hsU9HuDik"; | |
| useEffect(() => { | |
| const centrifuge = new Centrifuge(websocketUrl, { | |
| token: websocketToken, | |
| }); | |
| centrifuge.on("connect", function (context) { | |
| console.log("Connected to WebSocket"); | |
| }); | |
| centrifuge.on("disconnect", function (context) { | |
| console.log("Disconnected from WebSocket:", context.reason); | |
| // Retry connection logic if disconnected | |
| setTimeout(() => centrifuge.connect(), 5000); | |
| }); | |
| const subscription = centrifuge.newSubscription("orderbook"); | |
| subscription.on("subscribe", function (ctx) { | |
| console.log("Subscribed to orderbook channel"); | |
| }); | |
| subscription.on("error", function (ctx) { | |
| console.error("Subscription error:", ctx); | |
| }); | |
| subscription.subscribe(); | |
| centrifuge.connect(); | |
| return () => { | |
| subscription.unsubscribe(); | |
| centrifuge.disconnect(); | |
| }; | |
| }, [handleWebSocketMessage]); | |
| return ( | |
| <div>This is an order book</div> | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment