Xamarin.ios Web Socket with Rails cable

What is web socket?

Web socket is a communication protocol . It’s bit different TCP protocol from HTTP/HTTPS. Both protocols are located at layer 7. Web sockets are work over http ports 80 and 443 as well as to support http proxies and intermediaries.

What is Rails cable?

Action Cable seamlessly integrates WebSockets with the rest of your Rails application. It allows for real-time features to be written in Ruby in the same style and form as the rest of your Rails application, while still being performant and scalable. It’s a full-stack offering that provides both a client-side JavaScript framework and a server-side Ruby framework. You have access to your full domain model written with Active Record or your ORM of choice.

rails new websocket
class ChatsChannel < ApplicationCable::Channel  def subscribed 
stream_from “chat”
end
end
Rails.application.routes.draw do mount ActionCable.server => ‘/cable’end
config.action_cable.url = ‘ws://192.168.8.104:3000/cable’
config.action_cable.allowed_request_origins = [ ‘ws://192.168.8.104:3000’,’http://192.168.8.104:3000' ]
using System;
using WebSocket4Net;
using UIKit;namespace SocketApp{
public partial class ViewController : UIViewController
{
WebSocket websocket = new WebSocket(“ws://192.168.8.104:3000/cable”);
protected ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
}
}
}
public override void ViewDidLoad()
{
websocket.Opened += new EventHandler(websocket_Opened);
websocket.Error += new EventHandler<ErrorEventArgs>(websocket_Error);
websocket.Closed += new EventHandler(websocket_Closed);
websocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived);
base.ViewDidLoad();
}
private void websocket_Error(object sender, ErrorEventArgs e)
{
}
private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
{
}
private void websocket_Closed(object sender, EventArgs e)
{
}
private void websocket_Opened(object sender, EventArgs e)
{
}
 private void websocket_Opened(object sender, EventArgs e)
{
Dictionary<string, string> dictionary =
new Dictionary<string, string>();
dictionary.Add(“command”, “subscribe”);
dictionary.Add(“identifier”, “{“channel”: “UsersChannel”}”);
var j = JsonConvert.SerializeObject(dictionary); websocket.Send(j);
}
private void websocket_Opened(object sender, EventArgs e)
{
JObject o = JObject.Parse(e.Message.ToString());
if (o[“type”] == null)
{
InvokeOnMainThread(() =>
{
textView.Text = textView.Text + “\n” + e.Message;
});
}
}
partial void tapMessage(UIButton sender)
{
Dictionary<string, string> dictionary =
new Dictionary<string, string>();
dictionary.Add(“command”, “message”);
dictionary.Add(“identifier”, “{“channel”: “UsersChannel”}”);
dictionary.Add(“data”, “{“action”:”chat_message”,”msg”: “ + “”” + message.Text + “”}”);
var j = JsonConvert.SerializeObject(dictionary); websocket.Send(j);
}
class ChatsChannel < ApplicationCable::Channel def subscribed
stream_from “chat”
end
def chat_message(data) ActionCable.server.broadcast “chat” , success: ‘yes’, msg: data[‘msg’] endend

A Software architect