Cast Stream
Fetch stream of casts with Farcaster hubs
In this guide, we’ll create a stream of casts using Farcaster hubs and stream the casts published in real time.
Create a new node.js app using the following commands:
I have used bun but feel free to use npm, yarn, pnpm, or anything of your choice! Once the app is created, run this command to install the “@farcaster/hub-nodejs” package:
Now, let’s get to building our stream. In the index.ts file add the following to initialise the client:
You need to replace “YOUR_NEYNAR_API_KEY” with your API key. You can get it from your neynar app page.
Once our client is initialized we can use it to subscribe to specific events, in our case we want to subscribe to the MERGE_MESSAGE
event. You can check out the full details about the types of events here. So, add the following in your code:
Finally, let’s use the subscribeResult to stream and console log the cast texts:
We have to filter out the data by its type since the merge message events provide all protocol events like casts, reactions, profile updates, etc. 1 is for casts published.
Finally, you can run the script using bun run index.ts
and it will provide you with a stream like this:
Conclusion
Here’s what the completed code looks like
Lastly, make sure to share what you built with us on Farcaster by tagging @neynar and if you have any questions, reach out to us on warpcast or Telegram!
Was this page helpful?