Is there a way for a WebSocket client to send additional information on the initial connection to the WebSocket server?
I'm asking because I want the client to send the user ID (string) to the server immediately. I don't want the client to send the user ID in the onopen callback. Why? Because it's faster and simpler.
If the WebSocket API won't let you do this, why not? If there's no good reason, how could I suggest they add this simple feature?
Updated Answer
#dandavis is a genius. His comment on the question of sending the user ID in the query string of the first (url) argument of the WebSocket constructor works! And, I'm pretty sure it's only sent once by the client during the opening handshake (1.3) of the WebSocket protocol (RFC 6455).
It even worked to send it in the path, which I prefer for now since the WebSocket server I made is just for WebSockets. I'm using Node.js with ws. I'm connecting to the URL ws://localhost:5000/4, where 4 is the user ID. To get the user ID, I do ws.upgradeReq.url, like so:
wss.on('connection', function(ws) {
console.log(ws.upgradeReq.url); # => '/4'
});
Next, to make it secure, I'll pass the access token instead of the user ID.
Original Answer
You can pass the user ID (string) as the second (protocols) argument of the WebSocket constructor.
That should work, but it's not meant for what you want. It's definitely a hack. So, I don't recommend it. Also, the server will echo the user ID back to the client in its handshake. And, you don't need that. That would be extra data being sent over the wire that's of no use to you.
I'm not sure why the WebSocket API doesn't let you do what you want. Maybe it's more secure that way.
Since v3 ws.upgradeReq is deprecated and doesn't work anymore.
You can now use:
wss.on('connection', function(ws, req) {
console.log(req.url); # => '/4'
});
Source: https://github.com/websockets/ws#client-authentication
Related
I want to create a dashboard that automatically updates when new data is posted.
My first thought was to just make a javascript function and put a fetch statement in it and then loop the function every second or every couple of seconds...
Obviously, this is not a great solution. But I don't know what the better way is...
Some notes:
-PHP Server-Side Language
-Ran on Localhost so traffic is not going over the internet
Can anyone advise what I should be doing or if this is an acceptable approach?
Thanks in advance!
Server Side:
You can look for any onUpdate events if your database supports any such events
Or else just run a query in a timed interval to fetch new updates form the database (Connection to database is made just once and all subsequent requests go through the same connection. Hence this isn't a bad approach)
But when it comes to client side and receiving those updates, you can make it efficient in either of the two ways:
[Simple] Use Socket IO - Push an event with your new data and listen to them on the client side. (This way socket connection is made just once and all subsequent responses are received in the same connection)
Docs: https://socket.io/docs/v4/index.html
[Complex] Use HTTP stream
Example: https://gist.github.com/igrigorik/5736866
I am trying to add a function to a socket.io server which allows users to direct message other users.
I am able to get the target user's socket id and send it to the server, and I'm trying to use io.to(recipient).emit("dm", {description: message}); to send the message to the target. the issue I'm running into is that I can't seem to replace 'recipient' with a variable. when I try to, there is nothing sent to the client. The socket.io docs aren't great on this, I find no mention of being able to use a variable in an io.to function. does anyone have any information?
What is the best way to check if Twilio auht_token, account_sid are correct and sms can be sent, number checked? Some call which doesn't cost extra credits?
E.g. I see https://www.twilio.com/docs/api/rest/usage-records on RESTfull documentation but can't find how to get the same thing with JS SDK. Can't see dedicated endpoint for config checking so looking for anything else.
Environment: NodeJS 8.9
Twilio developer evangelist here.
Most API calls to the Twilio REST API don't cost, particularly those where you retrieve a resource or list resources. Since you mentioned SMS you could, for example, list your latest messages like this:
const client = require('twilio')(accountSid, authToken);
client.messages.list({ limit: 10 })
.then(function(messages) {
console.log("Everything is good!");
})
.catch(function(err) {
console.error("Something went wrong: ", err)
})
Take a look through the API reference and pick one that takes your fancy.
Using JS SDK might be insecure here. Because of that I think they didn't include a method in the JS API which may present the user the account_sid and the auth_token, which may be exploited. I assume you can use a server bridge between your client JS and Twilio API. Like this:
Client makes a JS AJAX request to http://my.domain.tld/checkstatus
Server connects to the Twilio API with C#, PHP, NodeJS or whatever tech it uses
Twilio returns that the credentials and tokens are still valid or expired
Server prepares the client response as true/false or 0/1
Client reads the status and continues or redirects somewhere else.
Edit There's a GET method here which you can also use with JS AJAX call:
https://www.twilio.com/docs/api/rest/usage-records#list-get
which is requested by this format:
/2010-04-01/Accounts/{AccountSid}/Usage/Records/{Subresource}
recently, external server(REST API) told me to use Keep-alive for my request. That's great, since I use request.js(node 8) I found "forever" option:
forever - set to true to use the forever-agent Note: Defaults to http(s).Agent({keepAlive:true}) in node 0.12+
To test if it's working I created my own server in Node8 using Koa.js and Node script where I create array of 100 GET request to my server and after that, I Promise.all them.
However, I don't how can I check if it's using the same agent, if it's working correctly.
Do you have any ideas, how from client(or server) point of view I can check that?
Is keep-alive enough proof that all request for certain domain are using the same agent in 1 Node.js process?
On the client, you can try to capture the socket which is used to send the request using the socket event. You can verify that they are using the same sockets on the client.
req.on("socket", function (socket) {
//if saved socket is empty save it
//else check if its using the saved socket
}
I would like to know if there is any way to retrieve CallSid using TwilioJS library from an outgoing call done from a Browser to a Phone. The think is that I'm not able to post anything from my client to my server since I don't have any id of the call.
I've tried to find out the parameters of the connection object:
var connection = Twilio.Device.connect()
But no CallSid appears there.
Does anyone know how can a get this parameter, or something that lets me to identify the ougoing call that I've just done from js library?
Thanks :D
As seen in https://www.twilio.com/docs/client/connection, connection should be a Twilio.Connection object which has a property named parameters with a CallSid attribute both for incoming and outgoing connections.
It is probably only available after the connection is made, so you should use an event handler for connect event:
Twilio.Device.connect(function(connection) {
var callSid = connection.parameters.CallSid;
});