Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works
Anything · Lazarus or Delphi
Random projectRandom project

pusher-websocket-delphi

Delphi library for interacting with the Pusher WebSocket API

View on GitHub

Member rating

No ratings yet.

Sign in to rate this project.

README

Pusher Delphi Client Library

This is a Delphi library for interacting with the Pusher WebSocket API.

Register at http://pusher.com and use the application credentials within your app as shown below.

More general documentation can be found at http://pusher.com/docs//

This library is based on Pusher .NET Client and uses the .NET lib as a DLL (see dotnet folder), so, this lib depends on .NET 4.5.

Tested on Delphi XE7 but should work in any XE+ version.

Looking for a server library? Take a look at pusher-http-delphi

Limitations

So far this lib only supports public channels. The .net library has support to privates and presence channels, so, the implementation shouldn't be so difficult.

Dependencies

  • Newtonsoft.Json.dll
  • WebSocket4Net.dll
  • PusherClient.dll
  • PusherClientNative.dll

All of them are shipped with this lib releases.

Usage

Download the last release zip package and add it to your project. Make sure all the dependencies are on the same folder that your exe.

Register the DLL using RegAsm:

pascal
%windir%\Microsoft.NET\Framework\v4.0.30319\regasm.exe /codebase PusherClientNative.dll /tlb:PusherClientNative.tlb

Add PusherClient to your unit uses clause.

pascal
PusherClient := TPusherClient.GetInstance;

PusherClient.Connect('your_pusher_key');

PusherClient.Subscribe('some_public_channel', 'some_event',
  procedure (Message: string)
  begin
    Log('[Event-Message]: ' + Message);
  end)

Remember to pay attention to the thread context in which you're running this code. The Log method implementation look like this (for use with VCL):

pascal
TThread.Queue(nil, procedure
  begin
    MemoLog.Lines.Add(Message)
  end);
end;

Secure Connections / SSL

SSL is enabled by default. You can disable it by passing a empty option list [] to the Connect method:

pascal
PusherClient.Connect('your_pusher_key', '', []);

Custom Host Address

It is possible to use a custom host address:

pascal
PusherClient.Connect('your_pusher_key', 'localhost');

The default value is ws.pusherapp.com which is the pusher.com endpoint, but you can also use it with a poxa server hosted in your own server.

Events

You can use some events to track the Pusher Client activity.

pascal
PusherClient.OnError := procedure(Message: string)
  begin
    Error('[ERROR]: ' + Message);
  end;
pascal
PusherClient.OnLog := procedure(Message: string)
  begin
    Log('[LOG]: ' + Message);
  end;
pascal
PusherClient.OnConnectionStateChange := procedure(Message: string)
  begin
    Log('[STATUS]: ' + Message);
  end;

Example

You can take a look at the PusherClientExample at delphi\example for more details.

Contributing

This lib is a work in progress and any help is greatly appreciated.

Found a bug? Send us a Pull Request or create an issue, we will do our best to help.

Acknowledgements

Thanks to @leggetter for the help on the pusher-websocket-dotnet which is the base of this work.