It sounds like you need HTTP client library. I do not see any reasons to implement HTTP protocol by your own over TCP-sockets and gen_tcp
. Erlang standard library already contains one called httpc.
Also there is plenty of third-party http clients for general purposes:
- lhttpc
- ibroswe
- hackney
For interaction with API I'd recommend you to take a look at gun. As project page says, it follows next goals:
Gun aims to provide an easy to use client compatible with HTTP, SPDY
and Websocket.
Gun is always connected. It will maintain a permanent connection to
the server, reopening it as soon as the server closes it, saving time
for the requests that come in.
All connections are supervised automatically, allowing the developer
to focus on writing his code without worrying.
If you need more general information about gen_tcp
module and network interaction at all, I'd recommend you to start from this great book.
UPD:
More specifically on twitter streaming API (do you mean this one?). To read streaming data you can use streaming interface of http client:
ok = application:start(inets).
{ok, Ref} = httpc:request(get, {RequestURL, RequestHeaders}, [], [{sync, false}, {stream, self}]).
After that you will receive following messages into process mailbox:
- First message will be
{http, {Ref, stream_start, ResponseHeaders}}
.
- Next messages will be
{http, {Ref, stream, ResponseBodyChunk}}
.
- Last message will be
{http, {Ref, stream_end, ResponseHeaders}}
.
After you've started such stream, you should just receive incomming messages via receive and handle it according to twitter streaming protocol.
Please, refer to documentation about httpc library. You really shouldn't use gen_tcp
sockets here at all.