I am a zig lover and I wonder how to use bun( the famous js runtime written in bun) in zig
I want to use the uwebsockets and boringssl in it to develop a web server
Can anyone help me?
Thanks
I’m a little confused about what you are trying to do. Are you trying to embed Bun in your code? Or are you trying to use the same libraries as Bun?
If you want to embed it, you’ll need to see if Bun exposes an api that you can pull in and leverage that. If you want to use the libraries, I would recommend looking at Bun’s build.zig
to see how they include it in their project.
Overall, we can help more if you can be more specific. If you have code examples or direct questions, you are more likely to get help.
Maybe you’re looking at it the wrong way. You can run Bun in Zig because Bun is a lot of things (a high level runtime, package manager, etc).
There used to be a C ABI for uWebSockets but that got removed because the folks (who worked at Bun, probably) stopped maintaining that part of the code and the maintainer removed it. If you want to use uWebSockets, then you’d have to check how Bun uses it and extract it out.
Thanks for your advice
You can see detailed information in my new post
Hey everyone,
I’m a zig lover and decided to write a web chat sever in zig.
But I’m new to zig web and I found bun (GitHub - oven-sh/bun: Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one) is a great project written in zig for web server, but just for js/ts.
And I found that in its deps lib there are zig bindings for uwebsockets and boringssl that I can use them for my zig code(these two libs are written in c)(bun/src/deps at main · oven-sh/bun · GitHub)
But I don’t know how to deal with dependency relation in that dir and how to use it in my own project. And I’m confused about how build.zig in bun works.
I asked bun community for help but no one reply me (that’s sad)
Thank for anyone who can help me!
And I will release a public project on github to use the libs in bun, if anyone can help me to solve this problem.
since the bun deps/ directory doesnt have build.zig in it, I think the best approach here should be to clone the files you want into your source code and try to use them from there.
There’s no need to start a new topic, just add the clarifying data here. I’ve combined the two topics for you. Welcome to ziggit
I think you may be biting off more than you can chew here. I took a look at the bun project, and their build system uses JS, CMake, and a little bit of Zig build system. It is not a great place to learn from if you are trying to learn Zig.
I would suggest boiling down your goal into something managable and build up from there. It seems like you are trying to build a Web server, potentially a websocket server. Why are you wanting to do this? To learn Zig? To use those libraries specifically?
My goal is to build a free chat app based on websocket by myself(not for business).
I had tried many different languages but I chose zig at last because I like its syntax and speed.
As you know, zig’s ecosystem isn’t big and there is no zig lib can achieve my goal(having ssl, websocket and http3 if can)
And uwebsockets has both ssl and websocket, even h3app, so I want to use it in my zig project.
Using c lib in zig, however, is not easy at this time. I can’t deal with dependency relations among uwebsockets, usockets, libuv, zlib that uwebsockets need. So I think bun have a c binding for uwebsockets and want to use it and I found that deps folder.
I want to know how that binding work and how do I use it. Could anyone help me with that?
Hi, i can reccomend looking at tardy-org.
It has one of the best implementatins of this kind of stuff.
Insanely performant, somewhat simple to use.
Bun is project written in Zig, but it targets JS, would not reccomend that.
Oh I tried zzz before, but it doesn’t support websocket
That is why i didnt specify zzz, to my knowledge tardy is able to support them. It even has “newly” separated secsock module.
Also this will overall be more streamlined once the IO changes land in std.
But it’s very hard to do that
It equals to build a new web framework
You didnt really explain how you want it to work.
You are talking about web sockets and zzz not being enough with its request based system, so i expect that you want to keep the connection alive for a long time?
In that case i dont know what you are talking about implementing the whole new Web Framework. You have a tardy with secsoc set up as server and listening on it (you can see the examples in secsoc repo). And on new connection just spin up a tardy “task” for it to run on and process the messages (you would have to do that in any language in some way, altho it might be a bit simpler in others).
You would have to write your front end for it, but that is something verry few frameworks do for you. (you can even serve your front end with ZZZ and then from its JS make connection to the secsoc, that i didnt do myself, but it should be possible)
What are you missing here, except for http 3, but for your usecase i would argue its unnecessary and even has some disadvantages. (http is not allways bigger number better protocol, depends what you are transfering)
I am not trying to push you towards using this exact stack, but if you dont like it, you can “import” some C library that does exactly what you want.
If there is something i am missing, please correct me.
Overall it still stands, that using Bun (from zig) might not be the best idea.
No, none of us are going to do it for you. Like you said, Zig is a newer language with a small ecosystem for now. So if you want something, your going to have to do it yourself.
Why don’t you try to build something first and then come here to ask for specific help instead of generic requests like this.
This is accurate. You are asking for something that doesn’t currently exist, so you are going to have to put it together yourself. If you want to use uWebSockets, try building it with the Zig build system. When you run into a specific issue, bring it back here for help. Then start using use uWebSockets in a small program and bring any issues here. Make sure to search for previous threads first.
We want to help, but none of us will solve your problem for you.
Another option is to try Zap, it’s supposed to be blazingly fast. I guess as fast (or faster) than Bun’s. If you don’t like their API, you could use the underlying C library (facil) which they use, and make your own abstraction from it.
Thank you Bobvan,
I will try to work with tardy-org
But write a websocket protocol by hand might be difficult,
do you have any experience?
It depends what you exactly want from the websocket protocol and if you even need it?
Because from what i understand, you want long living connection that can have message exchnage innitiated from the server.
First i would try to make it work just by using the secsocket and send some simple message encoding. (<usr>NAME</usr><msg>SOME-TEXT</msg> for example)
Once this kind of communication is working between your front-end / back-end (it is simple to implement) then i would try to upgreade to websocket encoding from your simple one, you can take for example the HTTP parser from ZZZ, since the first exchanges are HTTP-compatible. Not to mention you have SSL support handeled by the secsoc.
There is also large ammount of websocket protocol implementations in Zig. I just entered “websocket zig” on github and 4 popped up. You should be able to use or at least scavadge for parts some of them.
I found a new way to achieve it that is zuws(GitHub - sourlizi/zuws: Opinionated Zig bindings for uWebSockets) with my fork
but it’s quite slow however that I don’t know why