Sending a message directly form a Rails app to a Node.js and Socket.io push server - ruby-on-rails-3

I'm building a client side Browser app that uses a server side json API written in Ruby Rails 3 via AJAX. this all works great, no problems. but I want to push data to the users client when other users do things via the API with out having to have the client constantly be polling the server for new data.
so I set up a Node.js and Socket.IO app to act as a push server. Clients connect tot he Socket.IO server and subscribe to the channels they need to. but question is How can I get the Rails app to send a message to the Node server to be emitted on those channels?
I suppose I could just generate a post request to a special rout on the Node server but is there a better way? and how can I ensure that post request came form my rails app?

Use a Redis (redis.io) Pub/Sub channel to publish a message from your rails app, and subscribe to the Redis channel from the Node.js app.

Related

REST API with active push notifications from server to client

Problem description
i am working on a Xamarin application that consumes a REST API written in Python flask.
The Xamarin application offers virtual shopping lists where user can collaborate on buying stuff they have on a shared list.
To improve the user experience, i want to be able to actively notify the user about finished items on the list.
Possible solutions:
Synchronous API polling from client side
Notifications are stored by the API in a relational database and have a flag indicating if the user received the notification already.
The API has an endpoint GET /users/:user_id/notifications/ that queries the database for notifications and returns a JSON response with those.
Advantages
fairly simple to implement
Problems
synchronous polling creates a huge amount of http requests
API service remains stateless, making a horizontal scaling with a loadbalancer easier
Websocket endpoint on the API
The API has an endpoint POST /users/:user_id/notifications/register which creates a websocket connection between client and API.
The connection is stored to a global array in which each entry maps a client id to a websocket connection.
When a new notification is created, the endpoint makes a lookup in the connection dictionary by comparing the owner id of the notification with the dictionary entries. The notification is sent to appropriate user through the websocket.
Notifications are stored in the database like in the first approach.
When a user calls the endpoint, a new websocket connection will be established first and upon success the API sends all unseen notifications from the database to the user.
Advantages
API can push notifications to clients asynchronously
Problems
When a user terminates the websocket connection his dictionary entry will persis
Retaining one websocket connection per user permanently adds additional overhead to the API
Horizontal scalability of the API is more difficult because the service is not stateless anymore (Websocket connection information saved in
RabbitMQ
The API uses a RabbitMQ service to send notifications to the client. Every client uses subscribes to his own notification queue to prevent the broadcasting of messages.
Advantages
API remains stateless
Problems
Notifications needs to be resend to the exchange when a user is offline
Amount of queues grows drastically
Additional costs for RabbitMQ service
High temporary load on the RabbitMQ service when many users come online in the same time
Final words
It would be interesting to hear the opinion of others.
I believe the active distribution of notifications from backen services to clients i a very common use case.
best,
D
I would use RabbitMQ and consume events forwarding them as push notifications. This will work while the user is not actively connected to the website and enhance the engagement with each user experience that will return to the website when notified for more information see How to setup basic web push notification functionality using a Flask backend or How to send push notifications to a browser in ASP.NET Core or Sending Notifications with Spring Boot, Angular, and Firebase Cloud Messaging this way the RabbitMQ will not wait until the user is back online. If the user is online you can forward the notification directly to the Xamarin application via WebSockets and a load balancer like NGINX that can handle many WebSockets in an optimized way.
Synchronous API polling from the client-side is the less preferred way since it overloads the webserver with requests while nothing was changed.
I don't think the scalability of WebSocket is a problem. You can scale up easily with pub/sub. The hotspot of long connections is a kind of serious problem.
For one-way communication, I would suggest Server sent event. In the end, it usually depends on what your team is confident with.
I can recommend on a different approach for API that provides JSON which is called GraphQL
It supports subscriptions capabilities that are pushed by the GraphQL API Server (using web sockets)
GraphQL is considered today to be better than RESTful API since its very flexible and you can get exactly the data you need with one query.

Support for browser Push API from notification hubs

I’ve looked in multiple places, but cannot find details of how to register a web site against a notification hub for the sending and receiving of push messages.
The only like examples I have seen use a custom Node.js server for the web site to interact with.
Would really prefer to use a hub so we can send tagged messages to our browser app at the same time as our native apps.
It sounds like you want to register a web application to Azure Notification Hub to receive notification message. A web application can be as backend to send notification message using SDK in different languages like .NET, Node.js, Java, PHP, and Python. But the answer to receive notification message from web is absolutely not, there is an answer of the exising SO thread Can we register a webapplication to recieve notification from azure notification hub which has answered it.
Azure Notification Hubs are exclusively for push notifications for mobile platforms.
Only one exception is Google Cloud Messaging (GCM) supports Chrome Apps, please see the tutorial Tutorial: Push notifications to Chrome apps with Azure Notification Hubs
For sending and receiving Push Notification in browser, the only way is using Web Push API, you can refer to my answer of this SO thread Azure browser push notification for chrome, firefox ,and safari browser. And there is a Mozilla cookbook site for Web Push to show some examples to help getting started. Then, you can host your web push server on Azure. These Mozilla examples' backend are all using JavaScript based on Node.js, you can get the other web push libraries at this GitHub org web-push-libs if you want to use other languages.

XMPP Push Notification for iOS chat app

I'm creating Google talk client for iOS. I have BOSH server (used node-xmpp-bosh) and iOS client (used xmppframework). The iOS client connect through this BOSH server.
Now, how can I made a push notification to client (possibly from Bosh server) whenever there is incoming chat message or friend request?
Thanks!
node-xmpp-bosh actually provides for exactly this sort of use-case. The BOSH server is an instance of an eventpipe that lets you pipe events to/from the BOSH server to your custom modules/plugins. This is where you can code up the logic for the push notification service.
The advantage of doing this on the BOSH server is that you can support every XMPP service and not just the one that you control the XMPP server for. i.e. You can provide this functionality for google talk, facebook, jabber.org, etc...
Disclosure: I'm one of the authors of node-xmpp-bosh.
To support Push notification you need a special change on the server. We have developed that module for ejabberd (this is something you can see in TextOne and OneTeam).
The details to implement push notification yourself is available on:
http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html

How do I hook into new connections to ErlyVideo to run my own Erlang Authentication code?

I'm working on a video conferencing app.
We have a server running erlyVideo for internal video streaming.
We have a rest service on our web server for our erlyVideo server to call to validate session keys from our external site.
What we're stuck on is how to hook into new connections to pass the session key to the REST service to decide whether or not to terminate the connection.
Any thoughts?
What's wrong with using authentication described in their docs?
http://erlyvideo.org/authorization
You need to use apps_rewrite_play module:
{rtmp_handlers, [...., {apps_rewrite_play, "http://rest-service"},...]},

Implementation of a chat service using Restlet api

First off I'm not too familiar with restlets , just starting out. I wanted to implement a broadcast chatroom where a client sending a message would have the message broadcast to all other clients.
My attempt was to use a resource on the server side where the client would send the message(as a String) using POST. The other clients would constantly have to poll this resource to receive the message. I know this method must be horribly ineffective.
I was wondering if there was a better method where a change on the server side(in this case the sending of the string message) would result in the server alerting the clients of this update.
Some things will come in version 2.1 with the new nio connector. Within web page, you might consider using technologies like Comet or HTML5 web sockets.
See the specification page from the developer wiki of Restlet: http://wiki.restlet.org/developers/172-restlet/g3/354-restlet.html
Thierry