I need to update data(new offers or any other information) in a webpage. This update must be done by client and update must be displayed in the same webpage.
For eg: webpage called mirror.com and other webpage called mirror.com/update-
here,client will input data in mirror.com/update page and it must be displayed/updated in mirror.com page.
I'm using nodejs
Are you asking for real time updates? Then you have to use a Websocket connection.
You can use socket.io, sockjs or pusher.com for this. In the nodejs community , many people use socket.io. you can easily implement it into your project.
You have to build your own websocket server for socket.io and sockjs, but you don't want to do that when you use pusher. But socket.io, sockjs is much faster than pusher as i know. Because of when you use pusher , you have to connect to pusher server and grab the data back and then again you have to connect your own sever to display the data. But when you use socket.io or sockjs, you are working with the same sever.
Related
I am making a completely online website using node.js and react and I want to implement a forum in it. While saving the messages in the mongodb database how should I go about it so I don't have to make an http request for every single message as that will be bad economically. The chat will be implemented using socket.io and I interface with the database using mongoose.
I'm developing a react app and I need to trigger notifications in it for database changes , My back end is node and I'm not sure how to achieve this task. Should I listen always from front end for back-end notifications?I need to do It like how they do it in Facebook, When I develop my flutter apps I used Firebase streams to achieve this and don't know how to do this in react and node with PostgreSQL database.
You can use web sockets or socket.io library.
https://socket.io/
Old browsers don't support web sockets, in that case you need to check repeatedly from the front-end whether there is any notification from the back-end, lets say when database changes. This is called polling.
But, socket.io supports this polling automatically if browser don't support web sockets.
Socket.io is used by many applications . Your purpose seems to be solved using this library. It is event based. Once there is any database change in backend, and if you set up a socket.io event emitter, your front end will receive it via socket.io on the client and your react app can finally notify the user.
From their website,
Socket.IO enables real-time, bidirectional and event-based communication.
It works on every platform, browser or device, focusing equally on reliability and speed.
Is there any way using socket.io to share data between two different webpages?
Let's say I have page1.html and page2.html and they are acessed from different devices in the same network, and from page1 I send a string to page2.
From what I have seen I could make it using node.js and socket.io, based on the chat example from socket.io. However I'm not sure how.
Any help? Thanks.
There must be a socket server that handles all the web socket connections and events and so on...
The web pages cannot communicate directly but they must send data to the server. One important thing is how you plan to identify the clients.
I am building an app in which I provide functionality X, Y and chat.
Lets say that X and Y are non-interactive eg. reading articles - which will work fine with REST (on a node.js server) while chat is obviously interactive so it will work best with socket.io!
Questions: 1. Is it possible for me to 'switch on' a socket between the server and the user when the user navigates to the chat part of the application? 2. Can I open up a socket inside a GET request for the url: example.com/chats/usr_id on the node.js server?
3. How can this be accomplished inside a Backbone routing framework?
Yes. Just initialize the connection when the view is rendered (via a controller or script). See socket.io client documentation. You can just connect when the view is rendered and disconnect when the view is terminated. http://socket.io/docs/client-api/
You cannot open sockets with a GET request. Socket.io has it's own build in mechanisms for connecting to a socket server. It will start with Web Socket protocol and fall back to Long Polling. You can however use custom url's for unique things. One again, consult the socket.io documentation: http://socket.io/docs/client-api/
http://www.sitepoint.com/chat-application-using-socket-io/
p.s. I'd suggest reading up on how Web Sockets work, as you don't seem to have a very strong understanding.
I am writing a game in node.js with socket.io library. It has a server to whom 2 clients connect and play. My game is working fine, but there is a problem.
If a client that is already connected connects again to the game it is considered as 3rd connection request which messes things up.
This usually happens when I restart my node server when client browsers were not closed. I do I get around this.
You can use cookies. There are modules for this: Socket.IO Authentication
But you can also do it manually, as it is a simple cookie:
When a user connects, add a step to identify the user before starting the game. This step should create a cookie or use the existing one and send it to the server for authentication.
It's not difficult, you can read and write cookies with javascript, and sending a String/number to the server is not a problem either (websocket.emit('auth',whatever)).
Based on this example, it looks like its possible to use cookies to identify sessions in socket.io, but you may be better off using query string values to identify a particular browser or computer:
https://www.exratione.com/2013/05/the-use-of-cookies-versus-query-string-tokens-to-identify-sessions-in-socketio/