# Wrapping libwebsockets C Library with Julia

**URL:** <https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959>\
**Category:** Web Stack\
**Created:** [August 18, 2023, 4:44pm UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959 "2023-08-18T16:44:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![DeevsDeevs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deevsdeevs/32/52039_2.png) [@DeevsDeevs](https://discourse.julialang.org/u/DeevsDeevs)\
**Post date:** [August 18, 2023, 4:44pm UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959/1 "2023-08-18T16:44:35Z")

</div>

I’m wrapping libwebsockets in Julia for better integration and performance. Unfortunately, I didn’t find it in [JuliaBinaryWrappers](https://github.com/JuliaBinaryWrappers). After that i want to introduce simpler analogue for HTTP.jl based on this c lib (previously i tried libcurl, however, there is no websocket support). Do you have any advices? This is my first time wrapping lib myself (before that I used [libCurl\_jll](https://github.com/JuliaBinaryWrappers/libCURL_jll.jl))

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [August 18, 2023, 5:13pm UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959/2 "2023-08-18T17:13:25Z")

</div>

I’m sure that the _right_ thing to do is to use something like [Clang.jl](https://github.com/JuliaInterop/Clang.jl) to generate the wrapper for you. I don’t know much about it, even though I have drug my eyes over the documentation a few times.

If you are mildly masochistic, you can write Julia analogs of the structures used, and call all of the library functions from Julia using `@ccall`. I have a semi-large-ish C++ library that I have wrapped that way. I don’t have any experience with `libwebsockets`, but it looks semi-large. Probably large enough to make fussing around with Clang worthwhile.

Here is my example code (in [this Discourse link](https://discourse.julialang.org/t/advice-for-wrapping-c-library-that-throws-exceptions/101855)). Since you are wrapping C, you wouldn’t need to have a C library wrapper layer. I do recommend that you get bindings autogenerated, rather than doing it manually though.

---

<div class="post-metadata">

**Author:** ![DeevsDeevs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deevsdeevs/32/52039_2.png) [@DeevsDeevs](https://discourse.julialang.org/u/DeevsDeevs)\
**Post date:** [August 18, 2023, 5:17pm UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959/3 "2023-08-18T17:17:28Z")

</div>

And what about [BinaryBuilder](https://docs.binarybuilder.org/stable/jll/)? Or is this previous stage before using Clang.jl to generate wrapper?

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [August 18, 2023, 6:07pm UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959/4 "2023-08-18T18:07:53Z")

</div>

You may be right - I haven’t successfully used Clang.jl before.

Also, have you seen: [GitHub - JuliaWeb/WebSockets.jl: A WebSockets library for Julia](https://github.com/JuliaWeb/WebSockets.jl)?

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [August 18, 2023, 8:21pm UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959/5 "2023-08-18T20:21:05Z")

</div>

Both are kind of necessary steps. I prefer doing the binarybuilder one first because it tends to make the following steps a lot easier though nothing stops you from building the wrapper with a local build.

---

<div class="post-metadata">

**Author:** ![FemtoTrader](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/femtotrader/32/309_2.png) [@FemtoTrader](https://discourse.julialang.org/u/FemtoTrader)\
**Post date:** [November 10, 2024, 2:28pm UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959/6 "2024-11-10T14:28:53Z")

</div>

Any news @DeevsDeevs ?

I noticed [GitHub - maxfadson/libwebsockets\_jll.jl](https://github.com/maxfadson/libwebsockets_jll.jl) and [GitHub - JuliaBinaryWrappers/libwebsockets\_jll.jl](https://github.com/JuliaBinaryWrappers/libwebsockets_jll.jl) are now available.

---

<div class="post-metadata">

**Author:** ![FemtoTrader](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/femtotrader/32/309_2.png) [@FemtoTrader](https://discourse.julialang.org/u/FemtoTrader)\
**Post date:** [November 10, 2024, 2:50pm UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959/7 "2024-11-10T14:50:37Z")

</div>

I published a project skeleton for LibWebsockets.jl [https://github.com/femtotrader/LibWebsockets.jl](https://github.com/femtotrader/LibWebsockets.jl)

```julia
$ cd gen/
$ julia --project=. generator.jl

```

can generate code in lib/wrapper.jl

but I’m still locked trying to compile it using

```julia
cd ..
julia --project=. test/runtests.jl

```

with the following error

ERROR: LoadError: UndefVarError: `lws_plat_file_ops` not defined in `LibWebsockets.LibWebsocketsWrapper`

Feel free to look at it, fork and improve code.

---

<div class="post-metadata">

**Author:** ![JamesNZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jamesnz/32/35361_2.png) [@JamesNZ](https://discourse.julialang.org/u/JamesNZ)\
**Post date:** [November 11, 2024, 1:20am UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959/8 "2024-11-11T01:20:30Z")

</div>

Maybe you had an old version of the code loaded? For me the package loads and the tests pass.

---

<div class="post-metadata">

**Author:** ![FemtoTrader](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/femtotrader/32/309_2.png) [@FemtoTrader](https://discourse.julialang.org/u/FemtoTrader)\
**Post date:** [November 20, 2024, 6:51pm UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959/9 "2024-11-20T18:51:52Z")

</div>

> **[GitHub - bhftbootcamp/Libwebsockets.jl: Libwebsockets is a Julia wrapper for the...](https://github.com/bhftbootcamp/Libwebsockets.jl)**
>
> Libwebsockets is a Julia wrapper for the libwebsockets library, providing verstile tooling for setting up WebSocket and HTTP clients and servers, managing SSL connections, and handling data efficiently in a variety of applications

is now released.

That’s not my code (mine is now still available at [GitHub - femtotrader/WIP\_LibWebsockets.jl: Julia wrapper for https://libwebsockets.org/](https://github.com/femtotrader/WIP_LibWebsockets.jl/)). Thanks bhft guys for their contribution to open source.

---

<div class="post-metadata">

**Author:** ![DeevsDeevs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deevsdeevs/32/52039_2.png) [@DeevsDeevs](https://discourse.julialang.org/u/DeevsDeevs)\
**Post date:** [November 20, 2024, 8:22pm UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959/10 "2024-11-20T20:22:27Z")

</div>

my personal advice: just don’t use julia for anything web / trading related

---

<div class="post-metadata">

**Author:** ![FemtoTrader](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/femtotrader/32/309_2.png) [@FemtoTrader](https://discourse.julialang.org/u/FemtoTrader)\
**Post date:** [November 24, 2024, 9:18am UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959/11 "2024-11-24T09:18:48Z")

</div>

It sounds as a quite definitive answer. May I ask why?

---

<div class="post-metadata">

**Author:** ![g-gundam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/g-gundam/32/47593_2.png) [@g-gundam](https://discourse.julialang.org/u/g-gundam)\
**Post date:** [December 9, 2024, 6:49pm UTC](https://discourse.julialang.org/t/wrapping-libwebsockets-c-library-with-julia/102959/12 "2024-12-09T18:49:07Z")

</div>

I don’t know about him, but I don’t want to go back to a language without multiple dispatch. It makes me want to use Julia despite the lack of mature libraries in the algorithmic trading space. Just last night, I got a prototype of a strategy execution system working, and I used HierarchicalStateMachines.jl in combination with Rocket.jl to do it.

I’m a fan of state machines, and I find it very satisfying to see them work, because they can define behavior so precisely. However, this was my first time seeing a state machine implemented with multiple dispatch, and the satisfaction I got from seeing that is …probably a bit too much, but it’s so great. The code is so much smaller than what I had done in other languages in the past, and it was because I was able to dispatch on types from the Rocket side AND types on the HSM side at the same time. Without multiple dispatch, I couldn’t have done that. The only other language that might have allowed this kind of design is Common Lisp, but I like Julia more.

> I like the new kinds of design that multiple dispatch allows.

At first, it felt unfamiliar, but I got used to it quickly. When I tried to describe it to others, I called it “fine-grained polymorphism”. Things that might have been awkward to impossible in single dispatch suddenly became possible AND often simple too.

> There’s no going back.

This is too good to leave.
