Sending data back and forth between web client and Spark Core [Answered]

Hi, I’m currently using Node and Socket.io as the server and the client, and I assume I should set the Spark Core as another client to receive and send message, say:

// socket.io's wait on 'new message' event on http://00.00.000.000:8080/
socket.on('new message', function(data) {
        // broadcast the message to all clients                                                                       
        socket.broadcast.emit('new message', {
            username: socket.username,
            message: data
        });
});

I suppose if I set up the Core with TCPClient then it can receive broadcasted message and send out one just like other chat clients. Let’s say:

TCPClient client;
byte server[] = {00, 00, 000, 000};

void setup()
{
  Serial.begin(9600);
  while(!Serial.available()) SPARK_WLAN_Loop(); // What does SPARK_WLAN_Loop() do?

  Serial.println("connecting...");

  if (client.connect(server, 8080))
  {
    Serial.println("connected");
    client.write("Hello!"); // Will this message be sent to the server as another 'new message' event?
    client.read(); // Will this read whatever message broadcasted to all clients?
    client.stop(); // Will I have to disconnect from my Node server and connect back to Spark Cloud to execute other GET and POST requests?
  }
  else
  {
    Serial.println("connection failed");
  }
}

I assume the Core is by default a client connected to the Spark Cloud server, and by doing this I’m tapping it to my Node server from another source. Now, if I have to send normal GET and POST requests to the Cloud, will the Core still be able to sync to the Spark Cloud server after client.stop() automatically without having to Spark.connect()?

Heya @metaculus,

The core opens a socket by default to the Spark Cloud, but you can open up to 4 other TCP sockets to anything else, so you in general don’t need to worry about managing your cloud connection when you’re doing normal TCP stuff. :slight_smile:

Thanks,
David

1 Like

Ok @Dave considered SOLVED

1 Like

I’m trying to do the same exact thing, but having absolutely no luck. I have a node server running, with various clients (browsers, terminals, etc) connecting to it without issue.

I want to have the Spark Core connect as another client, and send/receive messages, but every time I try and get any kind of code running on it relating to TCP the core somehow gets corrupted and stops accepting new code being flashed to it. I’ve had to factory reset my core at leasts 10 times today, and I’m really disappointed so far.

What would it take to get this type of TCP client to actually run/connect to a running node server operating on port 80?

/Chris