Pass query params from node.js server to socket.io - javascript

I'm writing a node.js app, that can generate url's like this:
http://examle.com/?param1=val&param2=val
I wonder if users will follow this urls to my app - is this possible to get params from this url and pass it to socket.io socket object that represents connection with user that was brought to site by that url.
I.E.:
user followed this url;
node.js express server handles that url and get the query params from it;
Now I want to pass these params back to user but with socket.io emit(), not to whole sockets but only to user that was brought by url.
Is this goal achivable?
Should I use some id that will be passed with url and along socket.io handshake process and than store socket in array with id as key, to get this socket later in get request of express server?

Related

Send message to a part of clients using node.js + socket.io

I've been trying for some days to develop a real-time notification web app using node.js and the socket.io module.
WHAT I HAVE DONE
I have two parts.1) Admins and 2) Users.
So I have initialized two different namespaces on the client side.
Fo example: var socket = io(); for Admins and var socket = io('/Users'); for Users.The time a User sends a message, message goes to ALL of the Admins (1st step made). I am using this command to emit: nsp3.emit('chat message', obj);.nsp3 is var nsp3 = io.of('/Admins'); on the server and obj is a json structure.
WHAT I WANT TO ACCOMPLISH
When a message is sended by some User then the server receives the request, makes a SELECT query to db table based on some criteria and emit the message to a part of Admins who meet these criteria.
Which is the best way to do this?

Websocket on Reddit Endpoint to detect new post

I have a JavaScript script to check if there is a new post in a certain subreddit. Reddit magicaly provides a JSON endpoint on every link. In this case I have the following endpoint:
https://www.reddit.com/r/webdev/new.json?limit=1
I then used request module for node.js to get the specefic data I need eg domain, selftext, author and domain. However this changes every single time a new post is published and I therefore use a setInterval function to check every few seconds if a new post is released (Its like some sort of polling). I am saving every posts unique id to mongo to prevent double posting since that would assume every post is new on every request.
So the structure is something like:
setInterval(function () {
request({
uri: redditEndpoint,
json: true
}, function (error, response, body) {}) });
// I then save the data sent to mongo as unique to prevent double posting
// I consume the data here
}, 1000);
I would like to move away from this method and move over to websockets. However I am not sure how to correctly implement a websocket on such an endpoint, preferably with socket.io
That websocket endpoint would need to be provided by the server - in this case from the reddit server. You can't create a websocket connection to a random server which does not offer websocket support.
The only thing you could do is build a proxy server, which polls the reddit server for new events (like what you are currently doing) and which then offers a websocket endpoint itself for other clients.

Twilio: Where do the params from Twilio.Device.connect() get sent to

The Twilio JS library has a function called Twilio.Device.connect() which takes params. In the documentation it says these params get sent to the server, but it never specifies which server endpoint it goes to or how to set this up. https://www.twilio.com/docs/client/device
Can anybody explain?
Twilio evangelist here.
The parameters get converted into form encoded values and included in the HTTP request that is made to the URL configured for your TwiML App.
So for example, if you include parameters like this:
params = {"PhoneNumber": "+15555555555"};
Twilio.Device.connect(params);
they will get converted into this set of form encoded values and included in the POST request Twilio makes to your Twiml Apps Voice URL:
PhoneNumber=+15555555555
You can use whatever mechanism in your server side framework that exposes form values to grab those parameters and use them to generate and return TwiML. For example, in PHP:
$phoneNumber = $_REQUEST["PhoneNumber"];
Hope that helps.
Twilio provide web-hook for different events like when call initiated, ringing, completed etc, so you can get your custom parameter from web-hook ,
For Example Lets suppose you want channelId in server side, so First create one GET/POST API and assign to statusCallback url like in TwiML Bin
<Client
statusCallbackEvent='initiated ringing answered completed'
statusCallbackMethod= "GET" statusCallback="https://{{SERVER_ENDPOINT}}/twilio/peer-to-peer-call/{{channelId}}" >
{{To}}
</Client>
Now you can retrieve channelId as request params or request query in your server

Socketio - Changing data sent on connection

I'm trying to send userid from client side to server via socket io query string on the fly:
var socketio = io.connect(8080, {query : 'userid=100'});
And on server side, I use this id for authentication.
However, I'm a little worried about this. I thought that some malicious users may change userid and send to server their own id.
Am I thinking right? What should I do in this case?

Is there an equivalent of Netscape navigator functions in nodejs?

Can I access the inbuilt navigator functions like isinNet() or DomainNameorHost() from nodejs?
Since nodeJS runs on the server, not the browser, you can't access functions that are only provided in a browser.
Most developers use a middleware like Express to create a web service on nodejs.
In a route, such as
app.route("/play", function(req,res){
// code that handles URL /play
});
there is a callback function that is called when a request arrives for that route.
The req object parameter contains everything about the request.
req.ip is the upstream (incoming) ip address.
I looked around in npm for a module that might map remote ips to hostnames and could not find one. Presumably all it would do is reverseDNS, which could take time and hold up processing requests.

Categories

Resources