0

I am new to erlang and programming overall and I am looking for a way to connect to the twitter API by using sockets if there even is a way. So what I am wondering is, is there anyway to pass http requests/URL in sockets to do so?

I would love any information to read or look at. Example code where I can see gen_tcp, socket, ssl or whatever you think is the smartest way to go. (Of connecting to twitter/sending request).

I do not wish to use another built library unless I really need to.

Thanks in advance, Erlach

Erlach
  • 1
  • 1

1 Answers1

0

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:

  1. lhttpc
  2. ibroswe
  3. 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:

  1. First message will be {http, {Ref, stream_start, ResponseHeaders}}.
  2. Next messages will be {http, {Ref, stream, ResponseBodyChunk}}.
  3. 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.

Viacheslav Kovalev
  • 1,745
  • 12
  • 17
  • Thank you for the quick response. Does this mean you recommend I use httpc for connecting and sockets to "stream" what I get or am i mixing these things up? – Erlach Oct 26 '14 at 12:15
  • Each http client build on top of tcp socket (particulary, they already uses gen_tcp). This means that http client hides from you any tcp-specific details, and you interacting with http client interface ignoring any `gen_tcp`-specific details. – Viacheslav Kovalev Oct 26 '14 at 12:20
  • As basic exaple, you can try following: `httpc:request("http://stackoverflow.com")`. This call will return you HTTP response status, HTTP headers and page body. – Viacheslav Kovalev Oct 26 '14 at 12:21
  • Ok, so how would I go about it then if I wish to connect to the twitter api? Would I send an httpc request and then deal with incoming data in a socket recv? – Erlach Oct 26 '14 at 12:23
  • Im wondering I think the same as this question: http://stackoverflow.com/questions/6814695/looking-for-a-simple-ssl-erlang-example?rq=1 Because Im not really sure how I am supposed to go about using other thirdparty clients or libs that you suggest. – Erlach Oct 26 '14 at 12:37
  • I've updated my answer with example of streaming using httpc library. – Viacheslav Kovalev Oct 26 '14 at 12:50