curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Re: About a websockets write callback

From: Daniel F via curl-library <>
Date: Fri, 17 Jun 2022 19:22:21 +0200

W dniu 2022-06-17 13:39, Daniel Stenberg via curl-library napisaƂ(a):
> Hello,
> Last year when we brainstormed different takes on a WebSockets idea,
> we landed on a separate ws callback for writing data, similar to the
> A separate callback, because it needs to provide some additional data
> that the plain callback doesn't receive.
> Now when I've started working on implementing websockets, I'm having
> second thoughts on that design but I can't decide on what's a better
> way. So I'm here to see if someone else can fix this for me! =)
> I would like to provide websockets data with the CURLOPT_WRITEFUNCTION
> callback as well. But how would libcurl provide the additional
> necessary metadata?
> My initial thought was to provide a special websocket function that
> only can be used from within the callback that would provide the extra
> information (type of data, opcode, and if the FIN bit is set or not
> etc). Like maybe
> 'curl_ws_metadata()'.
> This is what makes me hesitate on this idea:
> This new function would need to get the easy handle as an argument, so
> that it can find and return the correct information, but the
> CURLOPT_WRITEFUNCTION does not pass in the easy handle! So in order to
> use this function within the callback, it would require that the
> callback on its own design gets the easy handle passed into it via the
> CURLOPT_WRITEDATA option in some manner.
> Would that be a sensible requirement or would be asking too much of an
> application?
> Or can we come up with a better way?
> [*] =

For me this new proposal is fine. In my apps I always pass pointer to
some structure or class to CURLOPT_WRITEDATA. and this structure/class
contains curl handle in addition to other data.

I can guess that apps written by others either pass pointer to
struct/class to CURLOPT_WRITEDATA, pass index from some array to
CURLOPT_WRITEDATA, or use single curl handle in whole app, which is
stored globally or in other well-known place. I do not see any special
issue in any of these approaches.

Make sure to properly document it with example how to use this. Full
info should be in docs for curl_ws_metadata function, plus some new note
WebSockets have extra requirement.

Received on 2022-06-17