Calling REST API on client side - searching for samples


#1

Hi!

I am writing my own simple embedded web-chat client on Vue.JS around RocketChat server.
I am utilizing websocket API and all is perfect with WS.

Now I want to provide my user with a full list of channels. So that my user could search for a channel and finally join it. Here I have got some questions:

  1. is REST API the only option for getting all the chat’s channels?

  2. could you provide some examples on how to: 2.1) query the “third page” of 50 channels 2.2) query the channels which names start with “Cha”

  3. when I write my code (Vue.JS) I am facing a CORS problem while accessing the chat. My authorized GET queries are always accompanied by OPTION queries (it’s up to browser so I can not manage it), therefore, they fail. I ended up “proxying” my Vue.JS REST API queries to my dummy server, that does nothing but requests the provided addresses with the data provided.

So, the question is: could you allow OPTION queries on the chat’s server side (while CORS is enabled, of course) so as to make developing around your REST API a less painful process?

Sorry for me probably being out of the topic…


#2

Hi @soarece, about your questions:

Which returns only channels, public channels in that case, the answer is YES. There are some methods in the Realtime API that you can consult.

2.1) I don’t know if you are talking about REST API, but if it is, you can see how you can count and offset through endpoints that support it. If the endpoints supports count and offset query parameters, they are described at the top of each endpoints docs, like that: https://rocket.chat/docs/developer-guides/rest-api/channels/list/.

2.2) To be able to search by names starts with, for example. You should use the query parameter, that allows you to use any MongoDB query, in your case, you should use the $regex to filter channels that starts with “Cha”.

I don’t know if I understood your question, but you can configure properly your CORS config in the admin panel, under Administration -> General -> REST API.