Create websocket without connecting - javascript

Is there a way to create a websocket without connecting to it right away?
So far I think
var ws = new WebSocket("ws://ws.my.url.com");
creates and then connects right away.
I'd like to create the socket, define the callbacks, and then connect once the user clicks a connect button.
Thanks

No. Creating a webSocket means you've created a connection to a server (or started the process of connecting). There is no such thing as creating a webSocket that isn't connecting yet.
You could create an object that would store all the configuration parameters and then just tell that object to connect when you wanted to, though I'm not really sure why that's better than just creating the actual webSocket when you the connection to be made.
Or just create a function that does all the setup work and call that function later when the user clicks a connect button.

Related

Sending HTTP messages to a specific client connection/web session using cURL/NodeJs scripts with SSEs

Hi so first I apologize if my query may seem unclear, it’s first trying to do what I’m doing and I haven’t full idea around the intricacies and lingo lol.
So basically I’m running a NodeJs web server with React handling my front end. I’ve got Express to help manipulate user sessions and I just came by Server-Sent-Events as a way to send one-way messages(which is what I need to do). So far I’m able to send updates and messages via cURL on the terminal and running JS scripts, however these updates/messages go to every active client session but I want/need to be able to send these messages to specific active client sessions/connections.
Example: 5 client connections are established (session IDs A,B,C,D,E), now I want to send an alert message to session E only and manually.
I’m still green with NodeJs/Express and the concept of SSEs however I’m learning as I go for this pet project.
Send help
What you want is how SSE works. It is a dedicated connection between a client and a server process.
however these updates/messages go to every active client session
If that is what you see then your node script is running the exact same code for each client.
I think your question might be higher up - how to organize the data messaging? That is too big a topic for a single StackOverflow question, because it will depend on so many factors specific to your use case.
But one way would be to have an SQL database, with one record for each user. The node script polls that database table and if the record for the current user changes, it sends the new data to them. Then to send data to user E, you just edit the database record for user E.

How to handle real time notification using WebSocket?

I've made real time chat app in React.js using Socket.io as milion of them in web. So now user A and user B can talk if they have open chat. I would like to add a new feature - when user A has open chat and user B has closed chat. When user A will send the message to user B he will get a popup/notification that he has new message and when he'll click on the notification he will open the chat and see the message.
I didn't found any solution here. Do U guys know how to solve this problem ? Is there any function in Socket.io to handle this ?
This is a very open question. By the way I'll try to explain the main concepts of a possible solution.
First of all, you need to consider that if you want real-time notification for "background-message" client B will need to keep connection open with socket (otherwise you'll need a sort of polling logic). Since B is connected to socket you can consider to work with Socket.io namespace in order to send this notification in another "room".
Another solution can be made by using Push Notification API provided by Firebase, AWS, etc.

How to check if database has been updated

I have this react app that periodically makes a fetch request to a database to update a list.
Is there a way to do this without using a timing loop ?
Block Diagram:
It depends on the technologies you are using.
The first thing you need to think is when the notification is created. If it really is created after a database change, the DB may need to trigger an event notifying it. Some databases offers this service, like Postgres, MySQL and even Firebase Realtime Database.
Once you identified the event, the best case is when you have a bidirectional connection between client and server, when the server can emit an event to the client. It's a common approach to use it with websockets, but sometimes you just need to emit an notification, and then, using Firebase Cloud Notifications, you can skip to have a WS server to handle this.
Otherwise, the only way is with long pooling.

How can I retrieve a list of websocket connections?

I'm working on a chatbot project where we open a websocket connection to communicate the chat messages.
Sometimes we get duplicate messages back and forth. I suspect that a second client is booted. I want to modify the boot() function so that it checks for existing websocket connections (and either cancel booting or close all other connections first).
Is there a way to get this from a vanilla javascript perspective? or do I maybe need to do something from the backend?
I tried to figure it out by looking at the code but without even a line number on which the error appears, it's pretty hard, anyways.
I suppose this is the problem here:
conn.send('USER_CONNECTED:' + conn.id); // send the connect message to ONE user
conn.broadcast('USER_DISCONNECTED:' + conn.id); // send the disconnect to ALL users
So it seems that you're simply missing a broadcast call when a user connects.

Creating database model with Mongoose

I have a node.js app that is essentially a sketchpad and currently I'm working on a feature to enable to save all of the sketches they've drawn during a "session" to a database so they can pick back up at a later time where they left off. I'm using a MongoDB database that I'm connecting to via the Mongoose ORM.
The server is started up in the file main.js which is currently where I'm opening the connection to the DB; however, the code for the saving of sketch data (which is currently just being saved to a JSON file on the server) is in a separate file. Based on this tutorial it seems that the code for the creation of models for a document are to go inside of a callback function that is run once the connection is open. But given that the logic for saving sketches in the app is in a different file from where the connection is being opened and since it says here that model instances aren't created/removed until the connection is open, it seems that there would either have to be a way to open different connections opened to create the models or that there would need to be a way to initiate the creation of the model for the sketches from the connection callback code in main.js.
I'm very new to MongoDB and Mongoose so I'm not sure if this is the correct way to think about creating models but given the needs of the feature, what would be the correct approach to opening the connection to the database and saving the sketches to the database once the save sketch function is called?
You may be overthinking this.
Just open your mongoose connection (a shared connection pool) via a mongoose.connect call during app start up and then create and save your Mongoose models whenever. Your models will use the shared connection pool as needed and will wait until the connection is established if necessary.

Categories

Resources