Basic questions of creating a real time api for mobile developer

Hello, I have read this page, and some video which wanted to create a real time chat in a web page with Phoenix channel help. But I never realized some things that are very basic and important.

question one:

when I just need to create a api for mobile developer, do I need to know js and write js for my project ? because I want to create a api, not html render?

question two:

if I don’t need js please tell me how to test my api which is created by websocket like postman app for json api.

question three:

I just found a lib for Mac Terminal that it’s name is wscat, but after some second it is terminated, and I cant test more than it.

I found heartbeats or there was something, that Kept my connection a live, but I didn’t find something more than it

at last, I think Phoenix channel is like elixir GenServer, but I can’t start and test a api to learn it, and this is a problem I have no easy sampel code to create a api just this not more.

if you have a code that Handles easy task like a registration user Api and how to test with some soft in mac or terminal not a mobile app, please give me and help me

Thanks

No, if there is no web UI then you do not need to care.

Write tests. There are also CLI tools for connecting to WS.

You need not only to connect to WS, but you also need to talk proper protocol, in this case Phoenix protocol defined in Phoenix.Socket.V2.JSONSerializer.

For testing channels you can use Phoenix.ChannelTest helpers.

1 Like

Thanks, I have a question about keeping a live a connection for user.

  • server should keep alive user with pinging user after some second?
  • or the user should ping server after some second?

imagine you have a chat application like Whatsapp or the better sample Telegram, how could you keep user connection?

Would you mind explaining me simple? please.

I read about heartbeat but I didn’t understand anything :cold_face:

The question is about Phoenix channel for mobile app without js

Using channels without js simply means you need an implementation of phoenix.js for your mobile app. Phoenix channels are not just raw websockets, but a communication protocol on top of many transports – websockets being one of them. Therefore your app needs to be able to communicate using that protocol. In detail this means being able to negotiate a serialization protocol for messages. Being able to serialize/deserialize messages according to said protocol, understanding a handful of channel specific messages as well as dealing with all the app specific messages. Then you’ll need facilities for handling channel joins/connection, dealing with replys and such. And if you need it (and it sounds like you do) you also need to deal with presence messages and the diffing mechanism.

As for tracking connectivity. Websocket connections will terminate once disconnected, but especially for flaky network this can be unrelyable for detecting disconnects. Therefore there are heartbeats as part of phoenix channels. I’m not sure currently if there’re initiated by the client or by the server though, but it’s likely the client.

2 Likes

Now this became clearer for me, Thank you, I think based on what I need, my user needs to use heartbeat, because if there is a problem server can’t create a new connection, but the mobile client how to use heartbeat?

I have read this page:

and I didn’t find anything about heartbeat in order to figure out how does they send me a heartbeat if the connection is online or not!! if not create a new!!! that I understand whether I need to create a function or not , or the phoenix handles it?

if you have sample code in swift please show me to know how it works!

thanks for explaining

if you search for heartbeat in that repo it’s there https://github.com/davidstump/SwiftPhoenixClient/search?q=heartbeat
I assume it’s handled automatically…

I haven’t done anything in swift but there is an example project there https://github.com/davidstump/SwiftPhoenixClient/tree/master/Example/ChatExample with specific code here:

1 Like