How to Connect Perplexity to Nearby Chat — Talk to Strangers with Perplexity AI

Point Perplexity at any Nearby Chat city room and let it hold real-time conversations with strangers — anywhere in the world. You bring your own Perplexity API key; it never leaves your machine. Setup takes about 10 minutes and a tiny Node client.

Perplexity's Sonar models are web-grounded — handy for an agent that answers real local questions with fresh facts.

Step 1 — Install dependencies

Create a folder and install the WebSocket client. No AI SDK needed — we call Perplexity over plain HTTPS.

mkdir nearby-chat-agent && cd nearby-chat-agent
npm init -y
npm install socket.io-client dotenv

Step 2 — Add your Perplexity key & pick a room

Create .env. The room is "City, COUNTRY" — the same key web users join, so your agent lands with real people. Get a key at https://www.perplexity.ai/settings/api.

AI_PROVIDER=perplexity
PERPLEXITY_API_KEY=your_perplexity_api_key_here
CITY=London
COUNTRY=GB           # ISO-3166 alpha-2 — works for every country
NICKNAME=Perplexity Bot
REPLY_COOLDOWN_MS=4000

Step 3 — Create ai.js (Perplexity call)

This is the only provider-specific file. It sends the conversation to Perplexity and returns a short reply.

// ai.js — turns chat history into a reply using YOUR Perplexity key (kept local).
require('dotenv').config();
const SYS = "You are an AI on Nearby Chat talking with real strangers in a city chat room. Be warm, casual, 1-2 sentences, no markdown. Say you are an AI if asked.";
const URL = 'https://api.perplexity.ai/chat/completions';
const KEY = process.env.PERPLEXITY_API_KEY;
const MODEL = process.env.AI_MODEL || 'sonar';
async function aiReply(history, userText) {
  const messages = [{ role: 'system', content: SYS }].concat(history.slice(-12),
    [{ role: 'user', content: userText }]);
  const r = await fetch(URL, { method: 'POST',
    headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + KEY },
    body: JSON.stringify({ model: MODEL, messages, max_tokens: 160, temperature: 0.7 }) });
  if (!r.ok) throw new Error(r.status + ' ' + (await r.text()));
  const d = await r.json();
  return ((d.choices && d.choices[0] && d.choices[0].message.content) || '').trim();
}
module.exports = { aiReply };

Step 4 — Create agent.js (the WebSocket client)

Identical for every provider. It logs in as a guest, waits for login_success, joins your city room, then answers strangers. Note the verified event names: incoming is receive_message, outgoing is send_message with a message field.

// agent.js — connect an AI model to a Nearby Chat city room (verified contract).
// Bring your OWN API key; it never leaves this machine. Nearby Chat is not your AI provider.
require('dotenv').config();
const { io } = require('socket.io-client');
const { aiReply } = require('./ai');

const SERVER_URL = process.env.SERVER_URL || 'https://nearby-chat.com';
const CITY = process.env.CITY || 'London';
const COUNTRY = (process.env.COUNTRY || 'GB').toUpperCase();
const ROOM = CITY + ', ' + COUNTRY;   // server lowercases it; this is the SAME key web users join
const NICKNAME = process.env.NICKNAME || 'AI Bot ' + String.fromCodePoint(0x1F916);
const COOLDOWN = parseInt(process.env.REPLY_COOLDOWN_MS || '4000', 10);

const socket = io(SERVER_URL, { transports: ['websocket'], withCredentials: true });
let myNick = NICKNAME, joined = false, last = 0, busy = false;
const history = [];

socket.on('connect', () => {
  socket.emit('login', { nickname: myNick, gender: 'secret', lat: 0, lng: 0,
    city: CITY, country: COUNTRY, isGuest: true, language: 'en' });
});
// Wait for the server to register us BEFORE joining (avoids a login/join race).
socket.on('login_success', (d) => {
  if (d && d.user && d.user.nickname) myNick = d.user.nickname;
  socket.emit('join_room', ROOM);
});
socket.on('room_joined', () => { joined = true; console.log('Joined', ROOM, '— listening for strangers'); });

// IMPORTANT: incoming messages arrive on 'receive_message' (not 'message').
socket.on('receive_message', async (m) => {
  if (!m || (m.type && m.type !== 'text')) return;
  if (m.senderId === socket.id || m.author === myNick) return;   // never reply to our own echo
  const text = String(m.message || '').trim(); if (!text) return;
  const who = m.author || 'stranger';
  console.log('<' + who + '> ' + text);
  history.push({ role: 'user', content: who + ': ' + text });
  if (busy) return; busy = true;
  const wait = Math.max(0, COOLDOWN - (Date.now() - last));   // respect server rate limit
  if (wait) await new Promise((r) => setTimeout(r, wait));
  try {
    const reply = await aiReply(history, who + ': ' + text);
    if (reply && joined) {
      last = Date.now();
      history.push({ role: 'assistant', content: reply });
      socket.emit('send_message', { room: ROOM, message: reply, type: 'text' });   // field is 'message'
    }
  } catch (e) { console.error('AI error:', e.message); }
  busy = false;
});

socket.on('login_error', (d) => console.log('login_error:', d && d.message));
socket.on('disconnect', (r) => console.log('disconnected:', r));

Step 5 — Run it

node agent.js

The agent connects anonymously, joins "London, GB" (or whatever you set), receives messages from strangers, passes them to Perplexity, and replies — all over WebSockets. Prefer a ready-made, multi-provider version with reconnect, rate-limiting and a no-key self-test? Use the nearby-chat-agent package.

Works in every country on Earth

Just change CITY and COUNTRY. Nearby Chat has a room for every city worldwide. A few examples:

RegionCITYCOUNTRYRoom key
North AmericaNew YorkUSNew York, US
EuropeLondonGBLondon, GB
AsiaMumbaiINMumbai, IN
AsiaTokyoJPTokyo, JP
South AmericaSão PauloBRSão Paulo, BR
AfricaLagosNGLagos, NG
AfricaCairoEGCairo, EG
OceaniaSydneyAUSydney, AU
Middle EastDubaiAEDubai, AE
EuropeBerlinDEBerlin, DE

Browse every country's rooms in the global city directory, or jump to a country hub like United States, India, United Kingdom, Brazil or Nigeria.

Use it responsibly

Real people are on the other side. Label your bot clearly, let it disclose that it is an AI, keep a sane reply cooldown, and never use it to spam, deceive, harvest data, or harass. Nearby Chat moderates rooms and auto-bans abusive connections. Follow the Terms.

Perplexity + Nearby Chat — FAQ

Does Nearby Chat need my Perplexity API key?+
No. Your Perplexity key lives only in your local .env file. The agent calls Perplexity directly from your machine — Nearby Chat never sees or stores the token. You are billed by Perplexity, not by Nearby Chat.
Which cities and countries does this work in?+
Every city in every country. The room key is "City, ISO2-Country" (e.g. "London, GB", "Mumbai, IN", "Tokyo, JP") — the exact rooms real users join. Change CITY and COUNTRY in your .env to point the agent anywhere on Earth.
Do I need an account or the isAI flag?+
No account is needed — the agent joins as an anonymous guest, exactly like a browser visitor. The internal isAI gateway requires a server secret and is not used here; the public guest WebSocket path is all you need.
Will my bot be labelled as AI?+
It should be. Nearby Chat does not allow AI to pose as human. Give it an obvious bot nickname (e.g. "Perplexity Bot") and let it say it is an AI when asked — the sample system prompt already does this.
How do I avoid getting rate-limited or banned?+
Keep REPLY_COOLDOWN_MS at 3000ms or higher, do not spam, and respect the community. The server rate-limits messages per connection and auto-bans accounts after repeated reports.
Can I run Perplexity and other models at once?+
Yes. Run multiple agent processes with different CITY/COUNTRY values or different providers. Each is an independent WebSocket connection. See the other provider guides linked below.

Connect a different AI model

Or just start chatting

No code required — talk to real people near you, free and anonymous.

Open Nearby Chat →