Send private message
Hi, how can I use this to send a message to a private socket id ?
I think you can consider following approach:
- When it's first to connect the socket server, let server remember authorization token from
Autorizationheader. and the server can bind this token to socket id. - Add some credentials (authorization token) in emitted header and client sends this to socket server.
- When the Socket IO server has received the header, parse header and auth token then send the private message.
I've created the PR #187 for this issue.
My approach is as follows:
- Store the client token and user to prepare for validation on the oncoming connection.
- I establish three connections and demonstrate these scenarios.
You can take look at more details about the client.php and server.js .
The socket IO server log will be the following output texts:
2019-04-11T06:19:24.135Z - info: SocketIO > listening on port 1337
2019-04-11T06:19:27.208Z - info: auth token this_is_peter_token
2019-04-11T06:19:27.210Z - info: SocketIO > Connected socket M5E21GSH8uWbkk9aAAAA
2019-04-11T06:19:27.210Z - info: X-My-Header websocket rocks
2019-04-11T06:19:27.221Z - info: ElephantIO private_chat_message > {"message":"How are you?","token":"this_is_peter_token"}
2019-04-11T06:19:27.221Z - info: ElephantIO private_chat_message > I am fine, peter
2019-04-11T06:19:27.223Z - info: SocketIO : Received 1 messages
2019-04-11T06:19:27.223Z - info: SocketIO > Disconnected socket M5E21GSH8uWbkk9aAAAA
2019-04-11T06:19:27.225Z - info: auth token this_is_peter_token
2019-04-11T06:19:27.225Z - info: SocketIO > Connected socket Yv8bbaA8-pR1OgKYAAAB
2019-04-11T06:19:27.225Z - info: X-My-Header websocket rocks
2019-04-11T06:19:27.228Z - info: ElephantIO private_chat_message > {"message":"Do you remember me?","token":"this_is_peter_token"}
2019-04-11T06:19:27.228Z - info: ElephantIO private_chat_message > I remember you, peter
2019-04-11T06:19:27.228Z - info: ElephantIO private_chat_message > {"message":"Do you remember me?","token":"this_is_invalid_peter_token"}
2019-04-11T06:19:27.228Z - info: ElephantIO private_chat_message > Token is invalid
2019-04-11T06:19:27.228Z - info: ElephantIO private_chat_message > Sorry. I don't remember you.
2019-04-11T06:19:27.229Z - info: SocketIO : Received 2 messages
2019-04-11T06:19:27.229Z - info: SocketIO > Disconnected socket Yv8bbaA8-pR1OgKYAAAB
@arafsan, if having any problem about sending messahe to private socket id, please write the comment on this issue :).
Hi, @peter279k I'm just trying to use elephant.io. I have tried https://github.com/Wisembly/elephant.io/issues/175#issuecomment-481983990 but I still got error like this:
C:\xampp2\htdocs\websocket_tutorial\elephant>node server.js 2019-10-24T07:12:54.198Z - info: SocketIO > listening on port 1337 2019-10-24T07:13:33.332Z - info: auth token this_is_peter_token 2019-10-24T07:13:33.395Z - info: SocketIO > Connected socket 9qTi3VIJekS4OZK5AAAA 2019-10-24T07:13:33.396Z - info: X-My-Header websocket rocks 2019-10-24T07:13:33.865Z - info: SocketIO : Received 0 messages 2019-10-24T07:13:33.867Z - info: SocketIO > Disconnected socket 9qTi3VIJekS4OZK5AAAA 2019-10-24T07:13:33.870Z - info: auth token this_is_peter_token 2019-10-24T07:13:33.872Z - info: SocketIO > Connected socket AjkDLor8gCnb1rZyAAAB 2019-10-24T07:13:33.873Z - info: X-My-Header websocket rocks 2019-10-24T07:13:34.281Z - info: SocketIO : Received 0 messages 2019-10-24T07:13:34.282Z - info: SocketIO > Disconnected socket AjkDLor8gCnb1rZyAAAB
Hi @awdynto, thanks for your reply.
I'm not sure what kinds of dependencies you use.
Could you provide your Node.js, SocketIO and PHP versions? Thanks.