centrifugal / centrifuge-android

Android client to communicate with Centrifugo v1 over Websockets (not maintained anymore)
48 stars 26 forks source link

no have send method ? #3

Closed RebortY closed 8 years ago

RebortY commented 8 years ago

i want to send message to centrifuge service , but library project Centrifugo.java no have sendMsg method ?

add sendMessage method in Centrifugo.java ?

FZambia commented 8 years ago

@yanglinglong1 hello. Are you looking for a method to publish message from your backend to Centrifugo? Or you want to publish message directly to Centrifugo from Android device?

The first way is idiomatic for Centrifugo because if you publish message directly from device then your backend will never receive this message (it just goes through Centrifugo to clients subscribed on channel). Unfortunately we have no Java client to publish messages to Centrifugo API - but it should be very easy to implement (see this chapter in docs - it's just a POST HTTP request to /api/ endpoint with JSON array of commands (publish command for example) in body and additional X-API-Sign header, if you protect your Centrifugo API endpoint from public access then you can start Centrifugo with --insecure_api flag and no signing required then). So you need to first deliver new message from device to your backend (using any convenient way for it) and the POST it to Centrifugo.

RebortY commented 8 years ago

@FZambia yes , I want to publish message to Centrifugo from Android device . I implement publish message API , tks very much .

FZambia commented 8 years ago

You are welcome! Just to clarify on example for future issue guests maybe, you have 2 ways to publish new message. Let's take an example of creating new message in chat.

1) Client writes message on Android device, you send it to your application backend, process it, validate, maybe save into database. Then publish to channel via POST HTTP request to Centrifugo API - it will be delivered to all clients subscribed on that channel. This is an idiomatic way. 2) There is possibility to publish message directly from Android device. Client writes message on device, you send it directly to Centrifugo (for example in Javascript browser client we have publish method for this). This only works for channels for which publish option set to true in Centrifugo configuration. In this case message will also be delivered to all clients subscribed on channel. But you lose a possibility to validate/save this message on backend side. So this way is mostly suitable for prototypes, demos, personal usage.

yan5 commented 8 years ago

@FZambia would be great if we can have a native way (I believe via websocket) to publish to Centrifugo server without going through Back-end (yes I understand, back-end won't be able to validate, but we our use-case can live without it). As an alternative, I understand we can publish over HTTP given that in config we set 'publish' = true, but what is the end-point we need to use? is it /api?

If so, (what I believe, please correct me if I'm wrong)

By the way, thanks for the awesome work, I'm really enjoying building one of my app with that.

FZambia commented 8 years ago

@yan5 sorry for late reply - in vacation at moment:)

I will ask @SammyVimes to add publish method to this client. Can not do this myself as I have no Java experience at all.

You are right you can use /api/ endpoint. By default every request to /api/ must be signed by secret key (you can look at our API client libraries to see this) so clients can not just use it without knowing that secret. Also in production setup this /api/ endpoint can be closed by firewall from clients. In this case it can be useful to turn off signing every API requests using --insecure_api option - as it on you dont need to sign every API request and can for example send API requests with CURL - just json command. Also its possible to run api endpoint on separate port if your firewall can not work with location based rules.

yan5 commented 8 years ago

@FZambia thanks for the reply.

Would be great if you could shed some lights on the other two questions.

No hurry, enjoy your vacation :)

FZambia commented 8 years ago

How does Centrifugo distinguish server (app back-end) request and client (android app) request?

Not sure I understand a question, Centrifugo knows from which handler request comes - websocket or HTTP. Under the hood those requests almost the same, the difference is in way of calling it, permissions, extra client info that's added to message on client direct publish, authorization mechanics.

What is point of setting 'publish'=true if both server and client sent do same API end point? Because how does Centrifugo knows if the request is really coming from client (mobile app for eg.)

publish=true option is for publishing over websocket connection from client without using application backend. It's suitable when you don't need to validate messages, save them and let clients communicate through Centrifugo without your backend. It's implemented in javascript client, but not implemented in Android client as this is not an idiomatic Centrifugo usage. But as I said above I will ask @SammyVimes to add this method to this client.