How to create a server socket in Ethereum? - node.js

I need my Ethereum app to receive messages coming from outside through an SSL socket. I have no idea how realize it. Have you got any suggestion? I was thinking of creating a socket inside the app.js, but how? Can anyone help me, by giving me some code examples?
Thank you.

Ethereum smart contracts are not able to invoke off-chain services for security reasons.
So, creating a socket client inside your app is not possible.
In Ethereum world, Oracles are used to provide off-chain data to smart contracts.
Take a look at this documentation for some details.

Related

Creating A Database Snapshot Listener

Forgive me if I'm heading down the wrong path here, if so, would be grateful if someone could point me in the right direction.
I'm curious about building a snapshot listener in Node/Express that returns database updates similar to how the snapshot listener on cloud firestore works.
For example, a front-end client would be able to listen through a single call, then receive updates in real-time without having to make additional calls.
For simplicity's sake, imagine for some reason we wanted to wrap Firestore's snapshot listener in a node/express function, then pass it onto the client and have identical functionality. How would you go about doing this, or am I totally wide of the mark?
Answering this as Community wiki. As mentioned in the comments,
Building your own persistent listener is definitely possible. If Firebase can do it, so can others.
Web sockets are an option indeed, but not required. Firestore's realtime listeners don't use web sockets for example, but the listeners on Firebase's other database (Realtime Database) do.

WebRTC through host with nodeJS express and socketio

I created a web app to let people communicate. I want to implement screen sharing and audio calls.
My current app is programmed in NodeJs and uses express and socket.io to serve the client connection and open a socket connection. I want to stream video and audio. My problem with WebRTC is that all those who connect to a call are vulnerable to a DDoS attack since it is p2p. I found an article from Discord explaining how they managed to let the entire traffic go through their servers: https://blog.discord.com/how-discord-handles-two-and-half-million-concurrent-voice-users-using-webrtc-ce01c3187429, that's exactly what I want to achieve.
Could I possibly use socket.io-stream https://www.npmjs.com/package/socket.io-stream ? I didn't yet figure out how, and it seems like all socket.io streaming libraries are made for file upload/download, not for actual video/audio streaming.
If that doesn't work, a library such as what Discord managed to make would be the perfect solution, since all traffic is proxied, and not p2p. Though I couldn't find any of those libraries, maybe I'm just looking for the wrong thing?
Best regards
You will want to use a SFU.
Each peer negotiates a session with the SFU. They then exchange media through it. Each Peer will just communicate with the server. It has lots of other benefits and is what most WebRTC deploys today use.
There are lots of Open Source SFUs out there. You can even build your own with Open Source libraries.

How to build a highly secure End to End Encryption React Native messaging app

I just posted this question on security stackexchange and they advised me to move my question to stackoverflow so here it is.
I am currently working on an instant React Native messaging app and I want to implement E2EE (End to End Encryption between the sender and the receiver) for better security. The libraries/frameworks I use are NodeJS for the backend, Socket.io for real-time communication, MongoDB for data management and obviously React Native for the frontend.
At this point, I am able to send messages back and forth from sender to the server and back to the receiver but the server can actually read the messages which is quite anoying because I want to save the messages (encrypted) in my database and retrieve them for the user to see his history.
Recently I found that the Diffie-Hellman key-exchange was a good solution to generate a shared secret key on each endpoint device but I don't know how to implement it in my app.
I also found that big messaging app (like WhatsApp, Facebook Messenger, Signal,... ) uses the Signal Protocol which is based on a X3DH (Extended Triple Diffie-Hellman) and I was wondering if it is possible to implement such a good thing in my RN app. But the problem is that even after reading the Signal Protocol's documentation I could not figure out how to implement it.
In conclusion my question is how can I implement the Signal Protocol in my RN app and uses Socket.io to send and receive encrypted messages? And if for some reason this is not possible, how can I implement the Diffie-Hellman key-exchange algorithm?
Thanks to anyone who can help me!

How to I manage messages of chat application when the application is offline

We are developing application for chatting.But when I am sending messages and if internet connectivity is gone how will I be able to send the messages like what's app do?
You have to use a message broker software like RabbitMQ (https://www.rabbitmq.com) to handle these kind of situations.
Off course, you can't really send messages offline, so what applications do is:
buffering/storing "sent" messages if there is no network available
check/await for network connection to be reestablished
Actually send messages when application reconnects
For achieving this, if you're looking mainly on chat, you can use some library/package/software (as for example RabbitMQ mentioned on #Badis Merabet answer).
If there is no prebuilt solution available for your use case or you want to develop you own solution, you may use PWAs. Here are some links:
General information on PWAs
Angular PWA docs
You may also check this answer for more information. The last link have a cookbook on an approach to implement it.

How to model Push Notifications on server

Brief Description:
Well, since many days I've been looking for an answer to this question but there seems to be answers for 'How to create a Push Notification Server' and like questions. I am using node.js and it's quite easy to 'create' a push notification server using sock.js (I've heard socket.io isn't good as compared to sock.js). No problem till here. But what I want is how to model such a server.
Details:
OK, so, let's say I've an application where there's a chat service (just an example this is, actual thing is big as you might have guessed). A person sends a message in a room and all the people in the room get notified. But what I want is a 'stateful' chat - that is, I want to store the messages in a data store. Here's where the trouble comes. Storing the message in the database and later telling everyone that "Hey, there's a message for you". This seems easy when we need the real-time activity for just one part of the app. What to do when the whole app is based on real-time communication? Besides this, I also want to have a RESTful api.
My solution (with which I am not really happy)
What I thought of doing was this: (on the server side of course)
Data Store
||
Data Layer (which talks to data store)
||
------------------
| |
Real-Time Server Restful server
And here, the Real-time server listens to interesting events that the data-layer publishes. Whenever something interesting happens, the server notifies the client. But which client? - This is the problem with my method
Hope you can be of help. :)
UPDATE:
I think I forgot to emphasize an important part of my question. How to implement a pub-sub system? (NOTE: I don't want the actual code, I'll manage that myself; just how to go about doing it is where I need some help). The problem is that I get quite boggled when writing the code - what to do how (my confusion is quite apparent from this question itself). Could please provide some references to read or some advice as to how to begin with this thing?
I am not sure if I understood you correctly; but I will summarize how I read it:
We have a real-time chat server that uses socket connections to publish new messages to all connected clients.
We have a database where we want to keep chat logs.
We have also a restful interface to access the realtime server to get current chats in a lazier manner.
And you want to architect your system this way:
In the above diagram, the components I circled with purple curve wants to be updated like all other clients. Am I right? I don't know what you meant with "Data Layer" but I thought it is a daemon that will be writing to database and also interfacing database for other components.
In this architecture, everything is okay in the direction you meant. I mean DataStore is connected by servers to access data, maybe to query client credentials for authentication, maybe to read user preferences etc.
For your other expectation from these components, I mean to allow these components to be updated like connected clients, why don't you allow them to be clients, too?
Your realtime server is a server for clients; but it is also a client for data layer, or database server, if we prefer a more common naming. So we already know that there is nothing that stops a server from being a client. Then, why can't our database system and restful system also be clients? Connect them to realtime server the same way you connect browsers and other clients. Let them enjoy being one of the people. :)
I hope I did not understand everything completely wrong and this makes sense for the question.

Resources