loopback 4 integration with socket.io - loopbackjs

I'm working on a project which is built on loopback 4, and now the client is asking to have notifications and messaging.
I searched a lot to find the best way to integrate socket.io with loopback 4, no luck, I found nothing
Any ideas?

LoopBack 4 does not have a first-class support for websockets yet. Raymond Feng, LoopBack's architect, created an example project to demonstrate how to route socket.io messages to Controller classes, you can find the project here:
https://github.com/raymondfeng/loopback4-example-websocket/
Quoting from the README:
This example is created to explore how to expose Websocket (socket.io)
endpoints in conjunction with LoopBack controllers.
Similarly as #loopback/rest, each websocket server is attached to an
http/https server. WebSocket controllers are mapped to different
routes (namespaces), for example:
/admins -> AdminController
/chats -> ChatController
When a client connects to the endpoint, a controller is instantiated
upon the connection event of the namespace with the socket object.
Controller methods can subscribe to one or more message types and send
messages to one or more clients.

Related

Connecting non-db, non-rest loopback connector to Loopback 4

Hoping someone can help me understand some Loopback 4 basics...
I'd like to connect a loopback connector that doesn't connect to a database or a REST endpoint. This particular library sends email using the sendgrid library. It's just basically a send function wrapped by a connector.
I'm not sure how to expose this function to my controller so that I can send emails, as the datasources require either 1. CRUD commands 2. REST endpoints, which doesn't apply to this connector.
I'm completely new to Loopback 4, so maybe I am just missing some basic knowledge which I can't find. Thanks
This particular library sends email using the sendgrid library. It's just basically a send function wrapped by a connector.
I'm not sure how to expose this function to my controller
Please follow the instructions in Calling other APIs and web services. Essentially:
Create a datasource for your sendgrid connector using lb4 datasource. Edit the created datasource JSON file and provide the necessary configuration like client credentials.
Create a new Service using lb4 service command.
Modify your controller - inject the Service via constructor arguments.
constructor(
#inject('services.EmailService')
protected emailService: CalculatorService,
) {}
In the controller methods, call the method provided by the connector via the injected service instance.
Alternatively, you don't have to use the connector and can call sendgrid library directly from your controller methods.

Django channels and socket.io-client

I'm trying to use them for the first time and wonder I'm headed to the right direction.
Here are my understandings,
socket.io is a wrapper around websocket, and falls back to sub-optimal solutions when websocket is not available.
Django channels can talk websocket as well.
(I think it converts django as a message queue like system. although this understanding or misunderstanding should affect this question)
So I'm trying to use Django channels on the server and socket.io-client on the client.
socket.io has api which looks like
socket.on(type, (payload)=> {})
Whereas Django channels has a form of
message.reply_channel.send({
"text": json
})
is the "text" type of socket.on(type)?
Can Django channels and socket.io-client talk to each other?
From the Socket.IO README:
Note: Socket.IO is not a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server (like ws://echo.websocket.org) either. Please see the protocol specification here.
So, you shouldn't expect Channels to work directly with Socket.IO. Global browser support for websockets is at 93%, which is probably high enough to just use the websocket API directly.
To quote the creator of django channels: https://github.com/django/channels/issues/1038
Channels doesn't support socket.io - it's a different protocol that isn't websockets or HTTP but layers on top of them. You'll have to use a socket.io server if you want to use it.

How to make Qt Websocket and QNetworkRequest (HTTP) to use the same connection?

Is it possible with Qt to upgrade a HTTP connection that handles the normal HTTP requests to a Websocket with the same connection?
I'm thinking about something like this with Poco libraries, but all done in Qt similar to QtWebApp.
The simple answer is no and that is mostly because of specifics of the server side. And Qt just follows the protocol available and exposed by the server (HTTP/WebSocket) as mostly the client-side development framework and AFAIK won't be able to do the kind of transformation you want of going from HTTP to Websocket that are two different protocols. But of course, theoretically that can be done as long as both protocols able to use IP port 80. But that implies new unique sever and new unique client implementations.
We use both WebSocket and REST in our app. And WebSocket is for triggering the client by the server to do something. Client gets the "poke" from the server and starts normal JSON HTTP-based exchange with the server.
Somewhat relative link: https://softwareengineering.stackexchange.com/questions/276253/mixing-rest-and-websocket-in-the-same-api

How to get a WebSocket server run on aws

I'm developing an iOS app that requires realtime dual-way server/client messaging.
I'm trying to use WebSocket++ to develop a WebSocket server app on an AWS EC2. Have to use C++ because that's the only language I know on the server side.
The problem is I'm a fresh guy on server side development. I have 2 very basic questions:
1, Do I need have to setup an HTTP server like apache/nginx in order to get websocket running?
That is, can websocket app live independently alone?
2, I have now setup an nginx server in case it is a must have, is there any resource that I can refer to to make nginx & websocket work together well?
No, you don't need a Web server, a (reverse) Web proxy or anything to have your C++ WebSocket server talk to WebSocket clients.
Nginx (as HAproxy) supports reverse proxying WebSocket. This can make sense in certain situations, like you want to terminate TLS at the proxy and forward plain WebSocket to your backend server, or you want to load-balance incoming WebSocket connections to multiple backend nodes. However, as said, this isn't required.
No you don't, websocket and socket for an HTTP server are two diffent things.
HTTP server is for the HTTP protocol while there is not protocol defined for websocket, you have to define it yourself typically by the mean of sending/receiving Json message (a stream of character which each side (the server and the client) knows how to read/write).
The goal of websocket is to offer to javascript through HTML5 an easy, light and quick way to communicate through a socket, without websocket you have to do that with web services and in that case you need a http server.
With websocket you can create an html file leveraging html tag and javascript, javascript use client side of websocket to communicate with a C++/websocket server program, and you do not need even a web server, in this scenario you have a "desktop web app" ! (here web term is only because you use html tags)
Same question, same answer, no again ;-)
Good luck, and welcome in the wonderful world of asio !

two ways communication in web services

How can I have two ways communication with WS. Two ways means a client could be a server and a server could be a client. As far as I understand the problem related to the client-server model in HTTP which is used by WS. What is the best practice for this scenario when a server wants send an event to multiple clients without being polled. ?
As far as I know there are some solutions but I do not know which one is best
1) server-push techniques (websockets)
2) SOAP over JMS (this sounds great)
3) WS-eventing
Thanks
The purpose of a webservice is - as the name says - to serve. It responds to requests, but it never sends requests on its own (but an application accessed through a webservice interface could send requests to other webservices to fulfill a request sent to it).
When a component of a service-oriented architecture is supposed to receive events from another component, it means that the receiving component has to act as a server and expose an own web services interface so that the component where the event occured can call it.