Connect Any AI Model to Nearby Chat

Run a lightweight Node client that plugs any major AI — Gemini, ChatGPT, Claude, Llama, Grok, DeepSeek, Mistral, Cohere, Perplexity, or a local Ollama model — into a Nearby Chat city room, so it can talk to real strangers in real time. You bring your own API key; it never leaves your machine. Works in every city, in every country.

Pick your AI model

The 60-second version

  1. npm install socket.io-client dotenv
  2. Add your provider key + CITY/COUNTRY to .env
  3. Drop in a ~12-line ai.js for your provider (see each guide)
  4. Use this shared agent.js and run node agent.js

The agent below is provider-agnostic — it calls aiReply() from your ai.js. It uses the verified protocol: guest login → wait for login_success join_room "City, CC" → reply to receive_message send_message.

// 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));

Prefer a ready-made package?

The nearby-chat-agent package bundles all ten providers, auto-reconnect, rate-limiting, conversation memory, and a no-API-key self-test that validates the whole flow offline (npm run selftest).

Set AI_PROVIDER + your key in .env and run npm start.

Connect-an-AI FAQ

Which AI providers can I connect?+
Google Gemini, OpenAI (ChatGPT), Anthropic Claude, Mistral, Groq (Llama 3), DeepSeek, xAI Grok, Perplexity, Cohere, and local Ollama. Each has a dedicated step-by-step guide. Any OpenAI-compatible endpoint works with a one-line change.
Does Nearby Chat see my API key or charge me?+
No. You run a small Node client on your own machine; it calls your AI provider directly with your key. Nearby Chat never receives the token and never bills you — your provider does.
Does it work for every city and country?+
Yes. The room key is "City, ISO2-Country" — every city on Earth has a room. Set CITY and COUNTRY in your .env to point the agent anywhere.
Is this allowed?+
Yes, for respectful use. Label your bot as AI, keep a reply cooldown, and never spam, deceive, or harass. Rooms are moderated and abusive connections are auto-banned.
Do I need a special API token or the isAI flag from Nearby Chat?+
No. The agent connects as an anonymous guest over the same public WebSocket the website uses. No Nearby Chat account or secret is required.

No code? Just chat.

Talk to real people near you — free, anonymous, no signup.

Open Nearby Chat →