I’m trying to connect to Twitter’s stream endpoint https://api.twitter.com/2/tweets/search/stream (docs) using WebSockex but it does not work.
Code
@url "https://api.twitter.com/2/tweets/search/stream"
@headers [
{"Authorization", "Bearer REDACTED"}
]
use WebSockex
def start_link(state) do
WebSockex.start_link(@url, __MODULE__, state, extra_headers: @headers)
end
def handle_frame({type, msg}, state) do
IO.puts("Received Message - Type: #{inspect(type)} -- Message: #{inspect(msg)}")
{:ok, state}
end
def handle_cast({:send, {type, msg} = frame}, state) do
IO.puts("Sending #{type} frame with payload: #{msg}")
{:reply, frame, state}
end
The socket fails to connect and returns %WebSockex.RequestError{code: 403, message: "Forbidden"}. I know my bearer token is valid because I am able to connect using curl e.g. curl https://api.twitter.com/2/tweets/search/stream -H "Authorization: Bearer $ACCESS_TOKEN".
For reference here are Ruby, Python & Javascript examples of connection to this endpoint`
I’m open to using any library to do this but WebSockex seems the most promising. I’ve tried quite a few other libraries as well.
Thanks for the feedback! To be honest I wasn’t clear if it was a socket or not but what you said makes sense so thanks for clearing that up. Ill take another look at ExTwitter but it doesn’t support using bearer tokens for auth, but I can probably learn from their implementation. In general would you say this guide is a relevant to what I’m trying to do? Download Large Files with HTTPoison Async Requests