Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] support websocket channel messages in Ponder #959

Open
hskang9 opened this issue Jul 5, 2024 · 0 comments
Open

[feature] support websocket channel messages in Ponder #959

hskang9 opened this issue Jul 5, 2024 · 0 comments

Comments

@hskang9
Copy link

hskang9 commented Jul 5, 2024

I think Ponder's greatest advantage over other web3 backend frameworks is that it can use other native javascript libraries, and I have found a way to integrate websocket for Ponder to deliver realtime update on next web3 consumer app.

Usage

  • Real-time ohlcv chart for buckets for candlestick charts as described in official docs.
  • Notification on app regarding order matching and price updates

So far here is how I integrate into Ponder.

First, add a file called Server.js and make code like this.

const server = require('http').createServer();

const io = require('socket.io')(server, {
  transports: ['trade', 'order', 'day', 'hour', 'min']
});

let tick = 0;
// 1. listen for socket connections
io.on('connection', (client: { emit: (arg0: string, arg1: { name: number; value: any; }) => void; }) => {
  
});

server.listen(process.env.WS_PORT);

export default io;

Then, inside on each ponder.on, make io emit an event with transport channel.

import io from "./Server";

ponder.on("matchingEngine:someEvent", async ({ event, context }) => {
  const { Analysis, Token, Pair } = context.db;
  const chainId = context.network.chainId;
  await PairAddedHandleTokenPairOrderbook(
    event,
    chainId,
    Analysis,
    Token,
    Pair
  );
  
  io.emit("trade", {...})
});

Is there any consideration on adding websocket inside the ponder to add io in the argument of ponder.on function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant