Rails. using a web socket like rest, but with an open connection - ruby-on-rails

I have a web api that posts json to different external endpoints on certain conditions. We have a new client that wants us to open a web socket connection with them during an event, send them the data (json) when we get it via this socket, and close the socket after the event. I'm having a hard time figuring out the rails way to do this.
How do I open a web socket connection and keep it open? (basically where would the client sit/ what would the definition look like).
How do I send messages over the socket from a controller? (eg. after processing a post request send new data to this websocket)
How do I close the connection?

You can try using this gem called Faye. Here's an example for connecting and sending data: (note: I didn't test it)
# Open the connection
ws = Faye::WebSocket::Client.new('ws://www.example.com/')
# Send data to connection
ws.send(YOURJSONDATA)
# Close connection (RFC 6455)
ws.close
Notes for faye:
Using the WebSocket client
Adding headers and other options to connection
WebSocket API
RFC 6455
Hope it helps!

Related

Creating a websocket with ngrok

I am using ngrok to tunnel to my ASP.NET core application. I am using Fleck to try and create a websocket connection between my application (server side) and an external API (client side). The problem is, the API requires a URL (ws://mysite.com/socket) that it will request a websocket connection from, however, the WebSocketServer(ip) object creates a websocket on a certain IP address and port. Can I tell the API to try and establish a connection with ws://5f1ce6fe.ngrok.io? Also, how do I find the IP address/port combination using ngrok that I will use to open the appropriate websocket on my server so that it accepts a websocket request at ws://5f1ce6fe.ngrok.io?
Thanks

Indy HTTP(s) reverse proxy

I'm trying to create a HTTP(s) proxy server that will act like this:
Browser configured to use proxy on 127.0.0.1:1080. On this port, I need to have a server (TIdTCPServer?) that will pass data coming to a client, that is connected on this same server (maybe into another port to make things easier). The client need to be connected, and not serving connection because of possible NAT restrictions.
The idea is that the client will execute the requests and send back to the server, which should send to the browser.
Browser > connect to local server > send to client connected which make the request
I thought about IdHTTPProxy, but no idea on how to pass request to a client, and client execute, gets back the data to the server / browser.
I read about TIdMappedPortTCP, but looks like this should connect into another host/port, and in my case the client can't receive connections.

What is socket hijacking?

I'm reading a great post on Rails 5 actioncable introduction. There it says: "Action Cable uses the Rack socket hijacking API to take over control of connections from the application server. ". What does the "socket hijacking" mean?
Socket Hijacking was implemented with rack 1.5.0 - a modular Ruby webserver interface.
Rack 1.5.0 basically provides a simple and adaptable interface for developing apps in rails. It does this by wrapping HTTP requests and their responses in a simply way. It also combines the API's for web servers, web frameworks, and middleware into a single method call.
So, in rack 1.5.0 socket hijacking is used to allow rails apps to overtake the client socket and perform other operations on it. These operations include:
Implementing WebSockets
Streaming data
Other interactivity between user's browser and server
WebSockets allows the user to send messages to a server and receive event driven responses without having to poll the server for a reply.
This is shown in this diagram - as you can see, once the WebSocket connection is opened, messages can be sent and received between the user and server.
Anyway, in the Rack Socket Hijacking API that you specified, it essentially provides two modes:
Full hijacking API
This gives the app complete control over what goes over the socket. The app server doesn’t send anything over the socket, and lets the app take care of it.
Partial hijacking API
This gives the app control over the socket after the app server has already sent out headers. This mode is basically used for streaming.
So - In the end, socket hijacking basically allows ruby/rails apps to override/overtake a client socket and carry out different functions on it, or as you wrote - take control of connections from the application server.

Sending data to a web Server

I've created a program that send's a data to a webserver. I used the NSURLConnection to send the data to the web server. I've created a php file on my server to handle all the POST request from the iPhone. My question was are there any other ways to send the data to a web Server besides using WiFi connection. I just only need to send small data.
are there any other ways to send the
data to a web Server besides using
wifi connection
Yes, EDGE and 3G

Asynchronous Socket Programming

I've created a project using socket programming. If I connect a single client to my server, the data I send from my client is successfully received by the server. But whenever I connect more than one client, only one client's data is received by the server. The other clients fail to connect. How can I make the server accept all new incoming connections, and how can I make the server receive two files from the same client?
If your code uses BeginAccept, EndAccept methods to accept new connection asynchonously, don't forget to call again BeginAccept after EndAccept call in your AcceptCallback.
Regards

Resources