Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send a message to my socket.io websocket from the command line in linux?

Is it possible to send a socket.io message to my localhost server (node) using the command line in linux?

My socket.io code looks like this:

var io = require('socket.io');
var socket;

socket = io.listen(server, {log: false});
server.listen(8081);

socket.sockets.on('connection', function (socket) {
  socket.on('message', function (data) 
  {
    console.log("Received socket message from GUI: "+data);
  }
});

I'd like to send a socket.io message that looks like this: "testControl,loadModels,start" which then triggers some server side logic. Is this possible?

I came across websocketd which seems promising, but it creates a websocket in linux, I just want to send a message to an existing websocket.

Version Info:

  • Node Version: v0.10.42
  • Socket.IO Version: 1.3.4
  • Express Version: 4.12.0
like image 335
Katie Avatar asked Feb 07 '17 23:02

Katie


People also ask

How do I send a message to WebSocket server?

send() The WebSocket. send() method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value of bufferedAmount by the number of bytes needed to contain the data.

Can you post to a WebSocket?

Sending Messages With WebSocketYou can send both text and binary data through a WebSocket. For your demo application you need to send the contents of the textarea to the server when the form is submitted. To do this you first need to set up an event listener on the form.


3 Answers

For a more low-level (socket.io-independent) solution you can use my tool websocat.

Example session for communicating with socket.io's chat example:

$ websocat 'wss://socket-io-chat.now.sh/socket.io/?transport=websocket'
< 0{"sid":"yGVElkNCXVgc5w_JOVtc","upgrades":[],"pingInterval":25000,"pingTimeout":60000}
< 40
> 2
< 3
> 42["add user", "websocat"]
< 42["login",{"numUsers":15}]
> 42["new message", "Hello from websocat"]
< 42["typing",{"username":"1223"}]
< 42["new message",{"username":"1223","message":"Reply from browser."}]
< 42["stop typing",{"username":"1223"}]
> 42["new message", "OK, may flood again."]

> denotes messages to be typed.

like image 102
Vi. Avatar answered Sep 21 '22 10:09

Vi.


You can use iocat cli client from npm which is based on Socket.IO.

$ iocat --socketio ws://127.0.0.1:8081
< Hello there
like image 39
Rjk Avatar answered Sep 19 '22 10:09

Rjk


I was able to work around this by doing the following:

  1. Create a new endpoint in your server

    app.post('/sendSocketMessage', function(req, res){
      console.log("Request is "+JSON.stringify(req.body));
      socketManager.parse(req.body); //send message directly to your socket parser
      res.status(200).send("OK");
    });
    
  2. Send data using curl

    curl --noproxy localhost, 
         -X POST 
         --data "data=testControl,loadModels,start," 
         http://localhost:8081/sendSocketMessage
    
like image 34
Katie Avatar answered Sep 17 '22 10:09

Katie