Publish with Javascript in Browser

PieSocket supports client-to-client communications. Which means you can publish messages directory from a client and build a serverless application. You need to enable C2C (client-to-client) communications for your API key from your PieSocket account, for this to work.

There are two ways to publish from a browser.

1. Using PieSocketJS

Include PieSocketJS into your application using NPM

npm install piesocket-js

or, using CDN

<script src="https://unpkg.com/piesocket-js@1"></script>

Then, use the following code to publish:

var piesocket = new PieSocket({
    clusterId: 'YOUR_CLUSTER_ID',
    apiKey: 'YOUR_API_KEY'
});
var channel = piesocket.subscribe(channelId); 

channel.on("open", ()=>{
  channel.send("Hello World!");
});

2. Publish using the WebSocket client

var apiKey = 'oCdCMcMPQpbvNjUIzqtvF1d2X2okWpDQj4AwARJuAgtjhzKxVEjQU6IdCjwm'; //Demo key, Change this to yours
var channelId = 1; 
var piesocket = new WebSocket(`wss://demo.piesocket.com/v3/${channelId}?api_key=${apiKey}&notify_self`);

piesocket.send("Hello world!");

Help

Facing difficulties? Use the chat box on the bottom-right corner of this page to reach us.