Monday, April 18, 2016

Where to start, implement long-polling in Faye for iOS

Leave a Comment

I'm using MZFayeClient (https://github.com/m1entus/MZFayeClient) using built in WebSocket. I need to implement long-polling technique. How to do it? Where to start? There isn't information (or enough information) on how to do it or if there an Faye library with long-polling.

2 Answers

Answers 1

I am not a IOS developer but one of my project I was helping to my IOS team for integrating the Faye server.

On my application we had a faye-server already running on rails framework for sending notification then we decide to sending notification to IOS device using same faye-server.

IOS team just follow the instruction of: https://github.com/pcrawfor/FayeObjC

self.client = [[MZFayeClient alloc] initWithURL:[NSURL URLWithString:@"ws://198.128.0.1:9292/faye"]];  ws://198.128.0.1:9292/faye // this URL is same which is used on web-app. 

subscribeToChannel & other methods are same like we are using on web-app.

Simple Faye Setup for Rails application .

How can I push to Faye Server from Rails Controller?

It will work for me I hope will also for you ..

sorry for my bad english ;)

Answers 2

1. Server up and running:

sudo rackup faye.ru -E production -s thin to start the Faye server

Thin web server (v1.6.4 codename Gob Bluth) Maximum connections set to 1024 Listening on localhost:9292, CTRL+C to stop

Check if server is working

curl http://localhost:9292/faye -d 'message={"channel":"/server", "data":"hello"}' 

Response

[{"channel":"/server","successful":true}] 

Server log:

[Faye::RackAdapter] Received message via HTTP POST: "{\"channel\":\"/server\", \"data\":\"hello\"}" [Faye::Server] Processing messages: [{"channel":"/server","data":"hello"}] (local: false) [Faye::Server] Passing through incoming extensions: {"channel":"/server","data":"hello"} [Faye::Server] Handling message: {"channel":"/server","data":"hello"} (local: false) [Faye::Engine::Proxy] Publishing message {"channel":"/server","data":"hello"} [Faye::Server] Processing reply: {"channel":"/server","successful":true} [Faye::Server] Passing through outgoing extensions: {"channel":"/server","successful":true} [Faye::Server] Returning replies: [{"channel":"/server","successful":true}] [Faye::RackAdapter] HTTP response: "[{\"channel\":\"/server\",\"successful\":true}]"

2. Work with client: Download MZFayeClient

cd FayeObjC-master/examples
pod install
open FayeObjC.xcworkspace

MZViewController.m

- (void)viewDidLoad {     [super viewDidLoad];      self.client = [[MZFayeClient alloc] initWithURL:[NSURL URLWithString:@"http://localhost:9292/faye"]];     [self.client subscribeToChannel:@"/server" success:^{         NSLog(@"ok");     } failure:^(NSError *error) {         NSLog(@"Server %@",error);     } receivedMessage:^(NSDictionary *message) {         NSLog(@"Server %@",message);     }];      self.client.delegate = self; } 

3. Build example: Touch on connect:

[Faye::RackAdapter] Received message via WebSocket[hybi-13]: "{\"supportedConnectionTypes\":[\"long-polling\",\"callback-polling\",\"iframe\",\"websocket\"],\"channel\":\"\/meta\/handshake\",\"minimumVersion\":\"1.0beta\",\"version\":\"1.0\"}" [Faye::Server] Processing messages: [{"supportedConnectionTypes":["long-polling","callback-polling","iframe","websocket"],"channel":"/meta/handshake","minimumVersion":"1.0beta","version":"1.0"}] (local: false) [Faye::Server] Passing through incoming extensions: {"supportedConnectionTypes":["long-polling","callback-polling","iframe","websocket"],"channel":"/meta/handshake","minimumVersion":"1.0beta","version":"1.0"} [Faye::Server] Handling message: {"supportedConnectionTypes":["long-polling","callback-polling","iframe","websocket"],"channel":"/meta/handshake","minimumVersion":"1.0beta","version":"1.0"} (local: false) [Faye::Engine::Proxy] Created new client "f68nwz5foyf1brczhmutdhclhnbtth5" [Faye::Engine::Proxy] Ping "f68nwz5foyf1brczhmutdhclhnbtth5", 45 [Faye::Server] Processing reply: {"channel":"/meta/handshake","successful":true,"version":"1.0","supportedConnectionTypes":["long-polling","cross-origin-long-polling","callback-polling","websocket","eventsource","in-process"],"clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","advice":{"reconnect":"retry","interval":0,"timeout":45000}} [Faye::Server] Passing through outgoing extensions: {"channel":"/meta/handshake","successful":true,"version":"1.0","supportedConnectionTypes":["long-polling","cross-origin-long-polling","callback-polling","websocket","eventsource","in-process"],"clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","advice":{"reconnect":"retry","interval":0,"timeout":45000}} [Faye::Server] Returning replies: [{"channel":"/meta/handshake","successful":true,"version":"1.0","supportedConnectionTypes":["long-polling","cross-origin-long-polling","callback-polling","websocket","eventsource","in-process"],"clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","advice":{"reconnect":"retry","interval":0,"timeout":45000}}] [Faye::RackAdapter] Received message via WebSocket[hybi-13]: "{\"channel\":\"\/meta\/connect\",\"clientId\":\"f68nwz5foyf1brczhmutdhclhnbtth5\",\"connectionType\":\"websocket\"}" [Faye::Server] Processing messages: [{"channel":"/meta/connect","clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","connectionType":"websocket"}] (local: false) [Faye::Server] Passing through incoming extensions: {"channel":"/meta/connect","clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","connectionType":"websocket"} [Faye::Server] Handling message: {"channel":"/meta/connect","clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","connectionType":"websocket"} (local: false) [Faye::Engine::Proxy] Accepting connection from "f68nwz5foyf1brczhmutdhclhnbtth5" [Faye::Engine::Proxy] Ping "f68nwz5foyf1brczhmutdhclhnbtth5", 45 [Faye::RackAdapter] Received message via WebSocket[hybi-13]: "{\"channel\":\"\/meta\/subscribe\",\"clientId\":\"f68nwz5foyf1brczhmutdhclhnbtth5\",\"subscription\":\"\/server\"}" [Faye::Server] Processing messages: [{"channel":"/meta/subscribe","clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","subscription":"/server"}] (local: false) [Faye::Server] Passing through incoming extensions: {"channel":"/meta/subscribe","clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","subscription":"/server"} [Faye::Server] Handling message: {"channel":"/meta/subscribe","clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","subscription":"/server"} (local: false) [Faye::Engine::Proxy] Subscribed client "f68nwz5foyf1brczhmutdhclhnbtth5" to channel "/server" [Faye::Server] Processing reply: {"clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","channel":"/meta/subscribe","successful":true,"subscription":"/server"} [Faye::Server] Passing through outgoing extensions: {"clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","channel":"/meta/subscribe","successful":true,"subscription":"/server"} [Faye::Server] Returning replies: [{"clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","channel":"/meta/subscribe","successful":true,"subscription":"/server"}]

Type "hello" then send

[Faye::RackAdapter] Received message via WebSocket[hybi-13]: "{\"data\":{\"text\":\"hello\"},\"clientId\":\"f68nwz5foyf1brczhmutdhclhnbtth5\",\"channel\":\"\/browser\",\"id\":\"MQ==\"}" [Faye::Server] Processing messages: [{"data":{"text":"hello"},"clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","channel":"/browser","id":"MQ=="}] (local: false) [Faye::Server] Passing through incoming extensions: {"data":{"text":"hello"},"clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","channel":"/browser","id":"MQ=="} [Faye::Server] Handling message: {"data":{"text":"hello"},"clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","channel":"/browser","id":"MQ=="} (local: false) [Faye::Engine::Proxy] Publishing message {"data":{"text":"hello"},"clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","channel":"/browser","id":"MQ=="} [Faye::Server] Processing reply: {"id":"MQ==","clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","channel":"/browser","successful":true} [Faye::Server] Passing through outgoing extensions: {"id":"MQ==","clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","channel":"/browser","successful":true} [Faye::Server] Returning replies: [{"id":"MQ==","clientId":"f68nwz5foyf1brczhmutdhclhnbtth5","channel":"/browser","successful":true}]

Hope this help.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment