PusherClient library problems

Hello

So, I’m doing a project on controlling my arduino with ehternet shield from an app on the web using Pusher (pusher.com). I have tried several libraries, and my problem is now, that I have two libraries:

No. 1 (https://github.com/krohling/ArduinoPusherClient)

  • capable of receiving messages and binding to (public)events
  • not capable of connecting/subscribing/authenticate with the pusher private channel

No.2 (https://github.com/marcoaltomonte/ArduinoPusherClient)

  • capable of connecting/subscribing to a private channel (creating auth key and socket-id)
  • not capable of binding to events after the subscription

To bind to an event No. 1 uses the following syntax:

#include <PusherClient.h>
PusherClient client;
  if(client.connect("your-api-key-here")) {
    client.bind("EventName", doSomethingFunction);
    client.subscribe("ChannelName");
  }
void doSomethingFunction(String data) {
Serial.println(data);
}

The “data” var is the data i receive from the event posting something.
Note that this is a public channel. To subscribe to a private channel i need to authenticate by receiving a key from pusher (when connecting), that i can use to generate a socket_id, that i then again can send to the server to finally be authenticated and subscribed to the private channel.

This problem someone tried to workaround like this(third library):

void pusherSocket() {
  if(pClient.connect(pKey)) {
    Serial.println("Oh yeah, we're in baby!");
    pClient.bind("pusher:connection_established", getSocketId);
  }  
}
//////////////////////////////////////////////////////////////////////////////////
void getSocketId(String data) {
  Serial.println("GENERATED!!!");
  socket_id = data.substring(data.lastIndexOf(':')+3,data.lastIndexOf('\\'));
}
////////////////////////////////////////////////////////////////////////////////////////////////
void getAuth() {
  Serial.print("SOCKET ID: ");
  Serial.println(socket_id);
  if(authClient.connect(serverName, 80)){
    authClient.print("GET ");
    authClient.print(location);
    authClient.print("?channel_name=");
    authClient.print(channelName);
    authClient.print("&socket_id=");  
    authClient.print(socket_id);
    authClient.println(" HTTP/1.1");
    authClient.print("Host: ");
    authClient.println(serverName);
    authClient.println();
    delay(500);
    boolean startRead = false;
    for(int i=0; i<300; i++){
      if (authClient.available()) {
        char c = authClient.read();
        if (c == '{'){
          startRead = true;
        } else if(startRead) {
          if(c != '}'){
            auth += c ;            
          } else {
            startRead = false;
            authClient.stop();
          }
        }
      }
    }
    String leader = "\"auth\":\"";
    if (auth.startsWith(leader)) {
      String nothing = NULL;
      auth.replace(leader,nothing);
      String trailer = "\"";
      auth.replace(trailer, nothing);
    }
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////
void pusherSubscr() {
  Serial.print("AUTH TOKEN: ");
  Serial.println(auth);
  pClient.subscribePrivate(channelName, auth);
}

Here the “pusher:connection_established” event that it listens to, should be a message that pusher would send right upon connection(and i can see that it connects and denied in the log of the pusher app). Though I found in the documentation that this return bang that pusher should send on event “pusher:connection_established” is not used anymore:

This document refers to version 1.9.0 of our library and above. Previous versions used the following events which have now been removed: pusher:connection_established and pusher:connection_failed.

http://pusher.com/docs/client_api_guide/client_connect.

So I’ve been trying to find out which connection event pusher is sending out instead… with no luck.

And while I was testing out different libraries I found a newer one (the No.2) which looks so much more simple, and it can subscribe and authenticate to private channels super easily. Just writing this:

#include <PusherClient.h>

And one thing here I simply don’t understand is that the example doesn’t include:

PusherClient client; // <--- not there?

But the example calls the class anyways like this, and everything works.

Pusher.subscribePrivate("private-ChannelName");
  Pusher.triggerPrivateEvent("private-ChannelName", "client-eventName", "\"\"");

And triggerPrivateEvent() is not defined in the No. 1 library.

But here I try to afterwards do something like this:

Pusher.bind("EventName", doSomethingFunction);
void doSomethingFunction(String data) {
Serial.println(data);
}

And the compiler says something like this:

private_channels.ino: In function ‘void setup()’:
private_channels:23: error: invalid conversion from ‘void ()(String)’ to 'void ()(const String&, const String&)’
private_channels:23: error: initializing argument 2 of ‘void PusherClient::bind(String, void (*)(const String&, const String&))’

Even though I can see that it’s in the source file of the library.

The difference of that two functions in the two libraries looks like this:
No.1:

void PusherClient::bind(String eventName, EventDelegate delegate) {
    _bindMap[eventName] = delegate;
}

Param: String eventName

No.2:

void PusherClient::bind(const String& eventName, EventDelegate delegate)
{
_bindMap[eventName] = delegate;
}

Param: const String& eventName

So maybe I need to put another thing as parameter in the second one to get it to work? But I can’t figure out what :confused:

Now I’m thinking of separating the two libraries to use one as a sender and the other one as a receiver, but there must be something that i didn’t see.

Hope you can help or give me any hints :slight_smile: Thanks in advance!

Peace!

And the sketch file i’m using is just the example for private channels in No.2 library with the added function. This is the one that compiles bad :confused:

#include <SPI.h>
#include <Ethernet.h>

#include <PusherClient.h>

void setup() 
{
  Serial.begin(9600);

  byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // mac adr
  if (Ethernet.begin(mac) == 0)
  {
    for(;;);
  }
  
  if (!Pusher.connect())
  {
    for(;;);
  } 
  
  Pusher.subscribePrivate("private-ChannelName");
   Pusher.triggerPrivateEvent("private-ChannelName", "client-eventName", "\"\"");
  Pusher.bind("client-eventName", something);

}

unsigned long lasttime;
bool toggle;

void loop() 
{   
  Pusher.monitor();  

  unsigned long time = millis();  
  if (time > lasttime + 1000)
  {
    signalActualState();
    lasttime = time;
  }
}

void something(String data) {
  Serial.println(data);
}

void signalActualState()
{
    if (toggle)
      Pusher.triggerPrivateEvent("private-ChannelName", "client-eventName", "\"1\"");
    else
      Pusher.triggerPrivateEvent("private-ChannelName", "client-eventName", "\"0\"");
}

Hey Pakken, I am stuck exactly at the same place. Were you able to figure this out?

Thank you in advance. Tree